在Rails 5.1中,所有表单都必须使用form_with
完成。在http://edgeguides.rubyonrails.org/5_1_release_notes.html#unification-of-form-for-and-form-tag-into-form-with中,我只能找到与模型相关的表单的示例。
在Rails 5.1中使用form_with
完成此Rails 5.0表单的正确方法是什么?
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
最佳答案
这是form_with
调用,它完全等同于问题中的form_tag
调用:
<%= form_with url: '/search', method: :get, local: true do |f| %>
<%= f.label :q, "Search for:" %>
<%= f.text_field :q, id: :q %>
<%= f.submit "Search" %>
<% end %>
请注意,默认情况下
form_with
是通过XHR(也称为remote: true
)发送的,您必须添加local: true
以使其表现得像form_tag
的默认remote: false
。在rails guides,API docs和this github issue discussion中查看有关它的更多信息。
关于ruby-on-rails - Rails 5.1中的form_with搜索字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43013354/