Javascript 在Rails 3中处理js.erb文件
我正在转换一个标准的scaffold生成的应用程序,以类似于中指定的方式使用AJAX和JQuery 我遵守了所有的指示:Javascript 在Rails 3中处理js.erb文件,javascript,jquery,ajax,ruby-on-rails-3,Javascript,Jquery,Ajax,Ruby On Rails 3,我正在转换一个标准的scaffold生成的应用程序,以类似于中指定的方式使用AJAX和JQuery 我遵守了所有的指示: 使用2个部分创建复合索引视图 更新控制器,仅保留索引、创建、编辑、更新和销毁操作 为使用JQuery函数更新DOM的创建、编辑、更新和销毁操作创建了js.erb文件 似乎根本无法访问js.erb文件。将js.erb与视图文件放在一起,例如app/views/customers/create.js.erb create.js.erb的代码是: <% if @custo
- 使用2个部分创建复合索引视图李>
- 更新控制器,仅保留索引、创建、编辑、更新和销毁操作李>
- 为使用JQuery函数更新DOM的创建、编辑、更新和销毁操作创建了js.erb文件
app/views/customers/create.js.erb
create.js.erb的代码是:
<% if @customer.errors.any? -%>
/*Hide the Flash Notice div*/
$("#flash_notice").hide(300);
/*Update the html of the div customer_errors with the new one*/
$("#customer_errors").html("<% @customer.errors.full_message.each do |msg| %>
<li><%= msg %></li>
<% end %>");
/*Show the div customer_errors*/
$("#cust0mer_errors").show(300);
<% else -%>
/*Hide the div customer_errors*/
$("#customer_errors").hide(300);
/*Update the html of the div flash_notice with the new one */
$("#flash_notice").html("<%= flash[:notice] %>");
/*Show the flash_notice div*/
$("#flash_notice").show(300);
/*Clear the entire form*/
$(":input:not(input[type=submit])").val("");
/*Replace the html of the div posts_list with the updated new one*/
$("#customers_list").html("<%= render "customers" %>";
<% end %>
创建
表单
和链接_到
对象时,需要确保对象上有:remote=>true
,否则路由将不会通过JS呈现操作。相反,它将使用默认HTML呈现它
例如:
<%= form_for(@post, :remote => true) do |f| %>
我不得不说,你正在读的教程是我读过的最差的教程。因为您似乎对RubyonRails非常陌生,所以我强烈建议您阅读不同的教程(如果您仍然想阅读关于AJAXW/Rails的内容,请阅读本教程,或者阅读一些非常好的教程,以便更好地了解Rails本身:).我已经把表单放上去了-您还需要什么?您可以在提交表单帖子或单击链接时发布服务器日志吗?另外,检查浏览器的控制台,看看是否有JS错误。把我能做的放到那里——还修复了create.JS.erb文件中的一些拼写错误。很高兴知道不止一个。您的
创建
控制器操作中仍然有一个错误:@customers=customer。所有
都应该是@customers=customer。所有
。。。还有,您的create.js.erb
文件中的另一个错误:$(“#customer_errors”).show(300)代码>应为$(“#客户#u错误”)。显示(300)
您还可以验证您是否安装了jQuery和jQuery Rails适配器(),并删除了format.html
引用吗?我之所以一直询问jQuery,是因为您的Rails服务器日志显示CustomerController的处理#create as html
(format.html),但是如果JS调用工作正常(通过jQuery),那么它应该由CustomerController#创建为JS
(format.JS)来处理
<%= form_for(@customer, :remote => true) do |f| %>
<div id="customer_errors" style="display:none"></div>
<div class="field">
<%= f.label :firstname %>
<%= f.text_field :firstname %>
<%= f.label :lastname %>
<%= f.text_field :lastname %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :phone %>
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
<%= f.label :address_id %>
<%= f.text_field :address_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Phone</th>
<th>Password</th>
<th>Address</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @customers.each do |customer| %>
<tr>
<td><%= customer.firstname %></td>
<td><%= customer.lastname %></td>
<td><%= customer.email %></td>
<td><%= customer.phone %></td>
<td><%= customer.password %></td>
<td><%= customer.address_id %></td>
<td><%= link_to 'Edit', edit_customer_path(customer), :remote => true %></td>
<td><%= link_to 'Destroy', customer, :remote => true, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
class CustomersController < ApplicationController
before_filter :load
def load
@customers = Customer.all
@customer = Customer.new
end
def index
end
def create
@customer = Customer.new(params[:customer])
if @customer.save
flash[:notice] = "Customer was successfully created."
@customers = Customer.all
respond_to do |format|
format.js
end
end
end
def edit
@customer = Customer.find(params[:id])
respond_to do |format|
format.js
end
end
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(params[:customer])
flash[:notice] = "Customer was successfully updated."
@customers = Customer.all
respond_to do |format|
format.js
end
end
end
def destroy
@customer = Customer.find(params[:id])
@customer.destroy
flash[:notice] = "Customer successfully destroyed"
@customers = Customer.all
respond_to do |format|
format.js
end
end
end
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' %>
<%= javascript_include_tag 'rails' %>
<%= csrf_meta_tag %>
Started GET "/customers" for 127.0.0.1 at Wed Dec 14 21:16:14 +0000 2011
Processing by CustomersController#index as HTML
Customer Load (1.3ms) SELECT "customers".* FROM "customers"
Rendered customers/_form.html.erb (14.1ms)
Rendered customers/_customers.html.erb (28.1ms)
Rendered customers/index.html.erb within layouts/application (47.6ms)
Completed 200 OK in 74ms (Views: 56.3ms | ActiveRecord: 1.3ms)
Started GET "/customers/13/edit" for 127.0.0.1 at Wed Dec 14 21:17:20 +0000 2011
Processing by CustomersController#edit as JS
Parameters: {"id"=>"13"}
Customer Load (1.1ms) SELECT "customers".* FROM "customers"
Customer Load (0.5ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = 13 LIMIT 1
Rendered customers/_form.html.erb (16.1ms)
Rendered customers/edit.js.erb (17.6ms)
Completed 200 OK in 43ms (Views: 27.6ms | ActiveRecord: 1.5ms)
Started GET "/customers/13/edit" for 127.0.0.1 at Wed Dec 14 21:17:31 +0000 2011
Processing by CustomersController#edit as JS
Parameters: {"id"=>"13"}
Customer Load (1.0ms) SELECT "customers".* FROM "customers"
Customer Load (0.3ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = 13 LIMIT 1
Rendered customers/_form.html.erb (25.9ms)
Rendered customers/edit.js.erb (28.8ms)
Completed 200 OK in 52ms (Views: 39.0ms | ActiveRecord: 1.3ms)
Started DELETE "/customers/18" for 127.0.0.1 at Wed Dec 14 21:18:31 +0000 2011
Processing by CustomersController#destroy as JS
Parameters: {"id"=>"18"}
Customer Load (1.0ms) SELECT "customers".* FROM "customers"
Customer Load (0.4ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = 18 LIMIT 1
AREL (0.4ms) DELETE FROM "customers" WHERE "customers"."id" = 18
Customer Load (0.7ms) SELECT "customers".* FROM "customers"
Rendered customers/_customers.html.erb (120.3ms)
Rendered customers/destroy.js.erb (122.1ms)
Completed 200 OK in 198ms (Views: 134.1ms | ActiveRecord: 2.5ms)
Started GET "/customers" for 127.0.0.1 at Wed Dec 14 21:20:00 +0000 2011
Processing by CustomersController#index as HTML
Customer Load (1.6ms) SELECT "customers".* FROM "customers"
Rendered customers/_form.html.erb (19.1ms)
Rendered customers/_customers.html.erb (23.8ms)
Rendered customers/index.html.erb within layouts/application (50.6ms)
Completed 200 OK in 76ms (Views: 54.9ms | ActiveRecord: 1.6m
<%= form_for(@post, :remote => true) do |f| %>
<%= link_to "Edit", edit_post_path(post), :remote => true %>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>