Rails 5.1 中的 form_with 搜索字段 [英] form_with search field in Rails 5.1
问题描述
在 Rails 5.1 中,所有表单都必须使用 form_with
完成.在 http://edgeguides.rubyonrails.org/5_1_release_notes.html#unification-of-form-for-and-form-tag-into-form-with 我只能找到与模型相关的表单示例.
In Rails 5.1 all the forms have to be done with form_with
. In http://edgeguides.rubyonrails.org/5_1_release_notes.html#unification-of-form-for-and-form-tag-into-form-with I can only find examples for forms which are related to models.
使用 form_with
在 Rails 5.1 中完成这个 Rails 5.0 表单的正确方法是什么?
What is the correct way for this Rails 5.0 form to be done in Rails 5.1 with form_with
?
<%= form_tag("/search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
推荐答案
这里是 form_with
调用,它完全等同于问题中的 form_tag
调用:>
Here is form_with
call, that is exact equivalent to the form_tag
call from the question:
<%= 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
.
Note that form_with
is sent via XHR (a.k.a remote: true
) by default, and you have to add local: true
to make it behave like form_tag
's default remote: false
.
在 rails 指南、API 文档 和 这个 github 问题讨论.
这篇关于Rails 5.1 中的 form_with 搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!