I will write an article for the first time. Record here what you learned about form_with while studying at school.
** Helper method ** for implementing the form.
A general term for methods for making HTML tags appear in views and processing text mainly in rails. HTML can do the same thing, but with the advantage of using helper methods.
python
<h1>New post page</h1>
<%= form_with url: "/posts", method: :post, local: true do |form| %>
<%= form.text_field :content %>
<%= form.submit 'Post' %>
<% end %>
Corresponding part ... <%= form.text_field :content %> <% = form.submit'submit'%>
Attributes can be added to the above relevant parts. What can be specified as an attribute **. 1 name attribute ** **. 2 value attribute ** **. 3 id attribute **
python
<%= f.text_area :tag_name, name:'item', value:@item.tags[0].tag_name, id:"tag-name" %>
**. 1 name attribute ** The character string defined by name is the key of the parameter sent in the HTTP request. Then, the character string entered in the form field is stored as value and sent. If there are multiple parameters, you can specify this to align them into one parameter.
**. 2 value attribute ** You can specify the initial value. You can enter that value originally.
**. 3 id attribute ** Specify with JavaScript.
And even if you do not describe the attributes introduced above, you can actually enter the code without attribute specification introduced at the beginning in the HTML file, and in fact it will be automatically replaced with ** input element ** behind the scenes The settings are correct. To check it, type the code and then open your browser's ** Verification Tool ** to check it. Therefore, I think that you should specify it in the case where you have to specify it. Also, to see the contents of form_with, enter ** bindin.pry ** directly under the create or update action and check the contents.
Recommended Posts