Form_with search field in Rails 5

form_with search field in Rails 5.1


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.


What is the correct way for this Rails 5.0 form to be done in Rails 5.1 with form_with ?


3 Answers 3


Here is form_with call, that is exact equivalent to the form_tag call from the question:


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 .


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


: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


Example of namespaced routes of form_with


For namespaced routes, like admin_post_url:


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:

Comments