form_with search field in Rails 5.1

前端 未结 3 417
一向
一向 2020-12-30 10:51

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

相关标签:
3条回答
  • 2020-12-30 11:03

    form_with is updated feature in rails 5.1 and its unified way to create form in rails it can be used as form_for or form_with its contain certain options

    :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.

    Example

    <%= 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) %>
    

    Example of namespaced routes of form_with

    For namespaced routes, like admin_post_url:

    <%= form_with(model: [ :admin, @post ]) do |form| %>
      ...
    <% end %>
    

    with associated resource

    If your resource has associations defined, for example, you want to add comments to the document given that the routes are set correctly:

    <%= form_with(model: [ @document, Comment.new ]) do |form| %>
      ...
    <% end %> 
    

    for more info checkout doc

    0 讨论(0)
  • 2020-12-30 11:08

    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 %>
    

    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.

    See more about it in rails guides, API docs and this github issue discussion.

    0 讨论(0)
  • 2020-12-30 11:08

    You may use form_with like this:

    <%= form_with(url: '/search') do |f| %>
      <%= f.label(:q, "Search for:") %>
      <%= f.text_field(:q, id: :q) %>
      <%= f.submit("Search") %>
    <% end %>
    
    0 讨论(0)
提交回复
热议问题