[RUBY] Understand the helper method form_with

What is form_with

form_with is one of Rails helper methods and is described in the view file. This method allows you to send information.

You can use form_with to create the required HTML for your input form.

When I looked it up, it seems that the description changes slightly when the sent information is saved in the database and when it is not saved.

** When not saving to database **

ruby:xxx.html.erb


<%= form_with url: "path" do |f| %>
Form contents
<% end %>

** When saving to database **

ruby:xxx.html.erb


<%= form_with(model:Model name, local: true) do |f| %>
Form contents
<% end %>

form_with does not save data You can also use input type elements It seems that you can also use both helper methods to save the data (such as f.text_field).

Finally, I will take an excerpt of what I actually implemented today and keep it as a record.

I hope it will be helpful as a description example.

ruby:xxx.html.erb


<div class="items-sell-main">
    <h2 class="items-sell-title">Enter product information</h2>
    <%= form_with(model: @product, local: true) do |f| %>
---abridgement---
    <%= f.text_area :name, class:"items-text", id:"item-name", placeholder:"Product name (required up to 40 characters))", maxlength:"40" %>
      <div class="items-explain">
        <div class="weight-bold-text">
Product description
          <span class="indispensable">Mandatory</span>
        </div>
---abridgement---
</div>

Recommended Posts

Understand the helper method form_with
How to use the form_with method
Consideration of Routing of form_with helper method
[Rails] About helper method form_with [Basic]
About the method
Output about the method # 2
[Rails] devise helper method
About the length method
About the authenticate method.
About the map method
About the ancestors method
[Rails] Completely understand form_with
About the to_s method.
Note the StringUtils # containsAny method
What is the pluck method?
Output about the method Part 1
about the where method (rails)
Understand the basics of docker
Let's understand the if statement!
Let's understand the guard statement!
Let's understand the for-in statement!
Let's understand the switch statement!
Consideration about the times method
Conditional branch with helper method
What is the initialize method?
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
I don't understand the devise_parameter_sanitizer method, so I'll output it here.
Easy to understand the difference between Ruby instance method and class method.
How to use the link_to method
Simulate the simplex method with GUI
How to use the include? method
Understand the basic mechanism of log4j2.xml
[java8] To understand the Stream API
Java comparison using the compareTo () method
Understand the difference between each_with_index and each.with_index
I tried to explain the method
Let's understand the Array (Element) type!
About the role of the initialize method
[Rails 6] destroy using the resources method
Was that so, the user_signed_in? method
[Ruby on Rails] Convenient helper method
Let's understand the Optional (Wrapped) type!
Call the super method in Java
How to decorate the radio button of rails6 form_with (helper) with CSS
[Rails / simple_format] Helper method that reflects line breaks entered in the form
[Rails] Articles for beginners to organize and understand the flow of form_with