[RUBY] [Rails] About helper method form_with [Basic]

I didn't understand much during the study, so I would like to organize it while outputting it. I will write carefully from the basics. If you point out, please.

What is a helper method?

In the first place, rails has a method for making HTML tags appear in view and processing text in advance. These are called helper methods. form_with is a type of helper method.

About form_with

A helper method for form implementation that has been recommended since the version Rails 5.1. form_tag / form_for seems to be deprecated. (The explanation of these two methods is omitted.)

.erb


<!-- form_Example using tag-->
<%= form_tag('/posts', method: :post) do %>
  <input type="text" name="content">
  <input type="submit" value="Post">
<% end %>

.erb


<!--form_Example using with-->
<%= form_with model: @post, local: true do |form| %>
  <%= form.text_field :content %>
  <%= form.submit 'Post' %>
<% end %>

The feature of form_with is (1) The path is automatically selected and there is no need to specify the HTTP method. (2) An instance of the model that inherits ActiveRecord passed from the controller can be used (@post corresponds to that in the above)

In this case, ① For new posts ② When calling an existing post The processing will change with.

① For new posts

posts_controller.rb


def new
  @post = Post.new
end

Click the post button and it will be sent to the create action.

new.html.erb


<%= form_with model: @post, class: :form, local: true do |form| %>
  <%= form.text_field :title, placeholder: :title, class: :form__title %>
  <%= form.text_area :content, placeholder: :Blog body, class: :form__text %>
  <%= form.submit 'Post', class: :form__btn %>
<% end %>

② When calling an existing post

posts_controller.rb


def edit
  @post = Post.find(params[:id])
end

edit.html.erb


<%= form_with model: @post, class: :form, local: true do |form| %>
  <%= form.text_field :title, placeholder: :title, class: :form__title %>
  <%= form.text_area :content, placeholder: :Blog body, class: :form__text %>
  <%= form.submit 'Post', class: :form__btn %>
<% end %>

Click the post button and it will be sent to the update action.

Summary

By comparison, the form parts of new.html.erb and edit.html.erb are the same! !! !! Therefore, the form part can be made into a partial template and the amount of description can be reduced. It will automatically determine if the model @post has contents and send it to you.

Besides this You can pass multiple models, for example form_with model: [@￰post, @￰comment]. I would like to write it in another article ...

Recommended Posts

[Rails] About helper method form_with [Basic]
about the where method (rails)
Understand the helper method form_with
About Rails scraping method Mechanize
Consideration of Routing of form_with helper method
[Ruby on Rails] Convenient helper method
rails method
[Rails] form_with
[Rails] About local: true described in form_with
[Rails] How to use helper method, confimartion
About Rails routing
Rails delegate method
About the method
[Servlet] Basic method
[Rails] About ActiveJob ,!
Rails basic philosophy
About Rails controller
About RSpec (Rails)
Output about the method # 2
About Android basic grammar
[Rails 6] About main gems
[Rails] About active hash
[rails] How to use devise helper method before_action: authenticate_user!
About rails application server
About rails kaminari pagination
About rails version specification
About Java method binding
About method splitting (Java)
MEMO about Rails 6 series
About the length method
About the authenticate method.
[Rails] devise introduction method
About the map method
About redirect_to in form_with
About the ancestors method
About form. ○○ of form_with
Delve into Rails form_with
[Java] Basic method notes
[rails] About devise defaults
[Rails] Completely understand form_with
About the to_s method.
Rails: About partial templates
About rails strong parameters
[Beginner] About Rails Session
Output about the method Part 1
[Rails] require method and permit method
Rails "render method" and "redirect method"
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
About naming Rails model methods
Basic knowledge in method definition
[Java Silver] About equals method
[Rails] About scss folder structure
Rails Asset Pipeline Basic Key
[Rails] About Rspec response test
Consideration about the times method
Conditional branch with helper method
Ruby on Rails Basic Memorandum