form_with Rails 5.1 中的搜索字段
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中我只能找到与模型相关的表格示例。
使用 form_with
在 Rails 5.1 中完成此 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_with(url: '/search') do |f| %>
<%= f.label(:q, "Search for:") %>
<%= f.text_field(:q, id: :q) %>
<%= f.submit("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 (a.k.a remote: true
) 发送,您必须添加 local: true
以使其表现得像 form_tag
的默认值remote: false
.
在 rails guides, API docs and this github issue discussion 中查看更多信息。
form_with
是 rails 5.1
中更新的功能及其在 rails 中创建表单的统一方式它可以用作 form_for
或 form_with
它的包含某些选项
:url - The URL the form submits to. Akin to values passed to url_for or link_to. For example, you may use a named route directly. When a :scope is passed without a :url the form just submits to the current URL.
:method - The method to use when submitting the form, usually either “get” or “post”. If “patch”, “put”, “delete”, or another verb is used, a hidden input named _method is added to simulate the verb over post.
:format - The format of the route the form submits to. Useful when submitting to another resource type, like :json. Skipped if a :url is passed.
:scope - The scope to prefix input field names with and thereby how the submitted parameters are grouped in controllers.
:model - A model object to infer the :url and :scope by, plus fill out input field values. So if a title attribute is set to “Ahoy!” then a title input field's value would be “Ahoy!”. If the model is a new record a create form is generated, if an existing record, however, an update form is generated. Pass :scope or :url to override the defaults. E.g. turn params[:post] into params[:article].
:authenticity_token - Authenticity token to use in the form. Override with a custom authenticity token or pass false to skip the authenticity token field altogether. Useful when submitting to an external resource like a payment gateway that might limit the valid fields. Remote forms may omit the embedded authenticity token by setting config.action_view.embed_authenticity_token_in_remote_forms = false. This is helpful when fragment-caching the form. Remote forms get the authenticity token from the meta tag, so embedding is unnecessary unless you support browsers without JavaScript.
:local - By default form submits are remote and unobstrusive XHRs.
Disable remote submits with local: true.
:skip_enforcing_utf8 - By default a hidden field named utf8 is output
to enforce UTF-8 submits. Set to true to skip the field.
:builder - Override the object used to build the form.
:id - Optional HTML id attribute.
:class - Optional HTML class attribute.
:data - Optional HTML data attributes.
:html - Other optional HTML attributes for the form tag.
例子
<%= form_with(model: @post, url: super_posts_path) %>
<%= form_with(model: @post, scope: :article) %>
<%= form_with(model: @post, format: :json) %>
<%= form_with(model: @post, authenticity_token: false) %>
form_with
的命名空间路由示例
对于命名空间路由,例如 admin_post_url:
<%= form_with(model: [ :admin, @post ]) do |form| %>
...
<% end %>
具有关联资源
如果您的资源定义了关联,例如,您希望在正确设置路由的情况下向文档添加注释:
<%= form_with(model: [ @document, Comment.new ]) do |form| %>
...
<% end %>
查看更多信息doc
在Rails 5.1中所有表格都必须用form_with
完成。在http://edgeguides.rubyonrails.org/5_1_release_notes.html#unification-of-form-for-and-form-tag-into-form-with中我只能找到与模型相关的表格示例。
使用 form_with
在 Rails 5.1 中完成此 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_with(url: '/search') do |f| %>
<%= f.label(:q, "Search for:") %>
<%= f.text_field(:q, id: :q) %>
<%= f.submit("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 (a.k.a remote: true
) 发送,您必须添加 local: true
以使其表现得像 form_tag
的默认值remote: false
.
在 rails guides, API docs and this github issue discussion 中查看更多信息。
form_with
是 rails 5.1
中更新的功能及其在 rails 中创建表单的统一方式它可以用作 form_for
或 form_with
它的包含某些选项
:url - The URL the form submits to. Akin to values passed to url_for or link_to. For example, you may use a named route directly. When a :scope is passed without a :url the form just submits to the current URL.
:method - The method to use when submitting the form, usually either “get” or “post”. If “patch”, “put”, “delete”, or another verb is used, a hidden input named _method is added to simulate the verb over post.
:format - The format of the route the form submits to. Useful when submitting to another resource type, like :json. Skipped if a :url is passed.
:scope - The scope to prefix input field names with and thereby how the submitted parameters are grouped in controllers.
:model - A model object to infer the :url and :scope by, plus fill out input field values. So if a title attribute is set to “Ahoy!” then a title input field's value would be “Ahoy!”. If the model is a new record a create form is generated, if an existing record, however, an update form is generated. Pass :scope or :url to override the defaults. E.g. turn params[:post] into params[:article].
:authenticity_token - Authenticity token to use in the form. Override with a custom authenticity token or pass false to skip the authenticity token field altogether. Useful when submitting to an external resource like a payment gateway that might limit the valid fields. Remote forms may omit the embedded authenticity token by setting config.action_view.embed_authenticity_token_in_remote_forms = false. This is helpful when fragment-caching the form. Remote forms get the authenticity token from the meta tag, so embedding is unnecessary unless you support browsers without JavaScript.
:local - By default form submits are remote and unobstrusive XHRs. Disable remote submits with local: true.
:skip_enforcing_utf8 - By default a hidden field named utf8 is output to enforce UTF-8 submits. Set to true to skip the field.
:builder - Override the object used to build the form.
:id - Optional HTML id attribute.
:class - Optional HTML class attribute.
:data - Optional HTML data attributes.
:html - Other optional HTML attributes for the form tag.
例子
<%= form_with(model: @post, url: super_posts_path) %>
<%= form_with(model: @post, scope: :article) %>
<%= form_with(model: @post, format: :json) %>
<%= form_with(model: @post, authenticity_token: false) %>
form_with
的命名空间路由示例
对于命名空间路由,例如 admin_post_url:
<%= form_with(model: [ :admin, @post ]) do |form| %>
...
<% end %>
具有关联资源
如果您的资源定义了关联,例如,您希望在正确设置路由的情况下向文档添加注释:
<%= form_with(model: [ @document, Comment.new ]) do |form| %>
...
<% end %>
查看更多信息doc