[英]Rails 3 not loading .js.erb files
I would like two .js.erb files, named "create" and "update" to load and be executed to replace a table in one of my views with an updated partial after a form is submitted. 我希望加载两个.js.erb文件,分别名为“ create”和“ update”,并在提交表单后执行,以用更新的部分替换我的视图之一中的表。
The files both contain the following (they are identical) 这些文件都包含以下内容(它们是相同的)
$('table#rating').replaceWith("<%= escape_javascript(render :partial => 'home/rating') %>");
However I am unsure where to put the .js.erb files (in the same view as my html.erb page?) and how to load/trigger them when the form submits. 但是我不确定.js.erb文件的位置(与html.erb页面位于同一视图中)以及如何在提交表单时加载/触发它们。 Any help would be appreciated. 任何帮助,将不胜感激。
Yes, you place .js.erb
files in the same location as the .html.erb
files you're using them with. 是的,您将.js.erb
文件放置在与使用它们的.html.erb
文件相同的位置。 In order for Rails to use them, you need to tell Rails that the form should be submitted asynchronously (via AJAX). 为了让Rails使用它们,您需要告诉Rails表单应该异步提交(通过AJAX)。 To do this, just add a :remote => true
to your form tag. 为此,只需在表单标签中添加:remote => true
即可。 For instance 例如
<%= form_for @some_model, :remote => true do %>
<!-- form stuff... -->
<% end %>
In order to submit a form with Ajax, you need to specify :remote => true
option as pointed out by Alex. 为了使用Ajax提交表单,您需要指定Alex指出的:remote => true
选项。
In the controller that handles the form submission should also be able to respond to Ajax request. 在处理表单提交的控制器中,还应该能够响应Ajax请求。 It should have something like following. 它应该具有类似以下内容。
class FormHandlingController < ApplicationController
def create
...
respond_to do |format|
format.js
end
end
end
You should place your craete.js.erb
under app/views/form_handling_controllers/
directory. 您应该将craete.js.erb
放在app/views/form_handling_controllers/
目录下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.