Rails 4 form_tag 更新元素与部分 [英] Rails 4 form_tag update element with partial
问题描述
在 Rails 4 中,如何调整下面的控制器、视图和部分视图,以便当我单击 "Update Time"
提交按钮时,ID 为 'search_results' 的 html div
是否使用 _results.html.erb
部分生成的内容进行更新?
我有一段时间没有使用过 Rails,我似乎记得在 Rails 1 中使用它非常简单.我为 Rails 4 找到的文档似乎涉及 JQuery,我不熟悉也不了解如何使用把它绑成这个形式.
我读过这个:http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html
但还是不完全明白是怎么回事.
RuleSearchController.rb
class RuleSearchController <应用控制器定义索引结尾定义结果结尾结尾
index.html.erb
<%= form_tag('/rule_search_results', :id => 'search_form') do %><%= submit_tag "更新时间" %><%结束%><div id='search_results'><%=渲染部分:结果"%>
_results.html.erb
<%= Time.new %>
更新当前的答案是在做一些奇怪的事情.当我在 _results.html.erb
中有一些东西时它会起作用,这是一个简单的单行代码,例如:
<%= Time.new %>
但是如果我做一些像这样简单的事情:
<%= Time.new %><小时/>
虽然控制台上没有出现错误,但没有任何反应并且搜索结果 div 没有更新.
进一步更新我可以迭代一个 AR 对象数组,并在 search_results div 中打印它们的名称,前提是我像下面这样执行它并且它是文件中的唯一行:
<% for rule in rules do %><%= rule.name %><% end %>
但是下面的不起作用
<% for rule in rules do %><%= rule.name %><%结束%>
这应该有效:
index.html.erb
<%= form_tag('/rule_search_results', :id => 'search_form', remote: true) do %><%= submit_tag "更新时间" %><%结束%>
然后在与您的 _results.html.erb 文件相同的目录中创建一个名为results.js.erb"的文件:
$('#search_results').html('<%= render partial: "results" %>');
它的作用是 remote: true 告诉控制器它不是一个 javascript 请求,因此控制器知道查找方法名称后跟 .js.erb 的文件并自动运行 JS 代码.
更新:为了解决多行 HTML 的问题,您可以在渲染之前添加一个 'j',这将转义输出:
$('#search_results').html('<%= j render partial: "results" %>');
In Rails 4 how do I adjust the below controller, view and partial view such that when I click the "Update Time"
Submit button, the html div with id of 'search_results'
is updated with content generated by the _results.html.erb
partial?
I have not used Rails in some time and I seem to remember it was extremely simple to do in Rails 1. The documentation I am finding for Rails 4 seems to involve JQuery which I am not familiar with and do not understand how to tie it into this form.
I have had a read through this: http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html
But still do not entirely understand what is going on.
RuleSearchController.rb
class RuleSearchController < ApplicationController
def index
end
def results
end
end
index.html.erb
<%= form_tag('/rule_search_results', :id => 'search_form') do %>
<%= submit_tag "Update Time" %>
<% end %>
<div id='search_results'>
<%= render partial: "results" %>
</div>
_results.html.erb
<%= Time.new %>
Update
The current answer is doing some weird things. It works when I have something in _results.html.erb
which is a simple one liner like:
<%= Time.new %>
If however I do something as simple as this:
<%= Time.new %>
<hr />
Whilst there is no error coming up on the console, nothing happens and the search results div is not updated.
Further Update I can iterate an array of AR objects and have their name print in the search_results div provided I do it in one like like below and that it is the only line in the file:
<% for rule in rules do %><%= rule.name %><% end %>
The below however does not work
<% for rule in rules do %>
<%= rule.name %>
<% end %>
This should work:
index.html.erb
<%= form_tag('/rule_search_results', :id => 'search_form', remote: true) do %>
<%= submit_tag "Update Time" %>
<% end %>
Then create a file called 'results.js.erb' in the same directory as your _results.html.erb file with this:
$('#search_results').html('<%= render partial: "results" %>');
What this does is the remote: true tells the controller than it's a javascript request, and so the controller knows to look for the file with the method name followed by .js.erb and run the JS code automatically.
Update: In answer to the issues with multi-line HTML, you can add a 'j' before then render, and this will escape the output:
$('#search_results').html('<%= j render partial: "results" %>');
这篇关于Rails 4 form_tag 更新元素与部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!