Form_with search field in Rails 5.1

In Rails 5.1, all forms must be executed using form_with. At 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 related to models.

What is the correct way for this version of Rails 5.0 in Rails 5.1 using form_with?

<%= form_tag("/search", method: "get") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>
+7
source share
3 answers

Here is a call form_withthat is exactly equivalent to a call form_tagfrom a 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 (a.k.a remote: true) , local: true, form_tag remote: false.

, API github.

+14

form_with ​​ rails 5.1 , form_for form_with,

: url. URL, . , url_for link_to. , . a: scope : url, URL.

: . , , , "get", "post". "", "", "" , _method .

: . , . , : json. , : url.

: . , , .

: . : url : scope by, . , title "Ahoy!" "Ahoy!". , , . Pass: scope : url . . params [: post] params [: article].

: authenticity_token - . false, . , , . , config.action_view.embed_authenticity_token_in_remote_forms = false. . , , JavaScript.

: . XHR. : true.

: skip_enforcing_utf8. utf8 UTF-8. true, .

: builder - , .

: id - HTML-.

: class - HTML.

: . HTML.

: html. HTML .

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

+5

You can use form_withas follows:

<%= form_with(url: '/search') do |f| %>
  <%= f.label(:q, "Search for:") %>
  <%= f.text_field(:q, id: :q) %>
  <%= f.submit("Search") %>
<% end %>
+1
source

Source: https://habr.com/ru/post/1016029/


All Articles