Form part attributes of form_with

I will write an article for the first time. Record here what you learned about form_with while studying at school.

What is form_with

The form_with method is ...

** Helper method ** for implementing the form.

What is a helper method?

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.

  1. To simplify the description such as path specification and Ruby embedding
  2. To solve security problems ** If you can write with helper methods, give priority to it. ** **

Form parts of form_with

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

Form part attributes of form_with

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

Form part attributes of form_with
About form. ○○ of form_with
[Meaning of model: of form_with]
Introduction of Docker --Part 1--
study of dbpedia part 25
[Active Admin] Color the title (panel) part of the form
[Form_with] Let's unify form with form_with.
Qualify only part of the text
Consideration of Routing of form_with helper method
Basic usage of java Optional Part 1