[RUBY] [Rails] Articles for beginners to organize and understand the flow of form_with


At the beginning of Rails learning, I could not understand the flow of form_with, so I will summarize it. Rails 5.2.4.

Summary first

I'm sure there are people who say, "Get together quickly!", So I'll summarize the flow first. スクリーンショット 2020-06-02 1.08.39.png

Brief commentary

There is a User model and a Post model.

** ① Display the view page (new.html.erb) with new action ** When a request to call the new action is made, the new post page of post is displayed first. ※image 73ee6ee22fb145fb7034907eef85317e.png

** ② Pass the @post instance created by the new action to the view **


class PostsController < ApplicationController
  def new
    @post = Post.new #Generated here@post is new.html.Passed to erb

** ③ Enter a value in the instance property and submit ** f3253df790131a6ac795eeff52d4ee5d.gif

** ④ Call the create action ** As you can see in the image above, when you click the submit button (submit form_with), form_with will call the create action.

  • The form_with helper method is a convenient child that looks at the properties of the passed instance (@post in this case) and decides whether to skip to create or update.

form_with is thinking

- @post is empty → I'm making it for the first time! Then create!
- @post is not empty → I want to change it! Then update!

** ⑤ Call the post_params method **


class PostsController < ApplicationController
  def create
    #The create action is called and Post.create(post_params)Is executed → post_params is called
    redirect_to root_path

  def post_params 
    params.require(:post).permit(:title, :content).merge(user_id: current_user.id)

** ⑥ Of the values received by params, specify the value received by permit (permit) **

State of the value received by params

[1] pry(#<PostsController>)> params
=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"oLGgLMTcqSPi7cbK9j1wFhH5rqiwXYO7GyRoZcZFZe6Y5VdbHrWCOyeo37kgW/bsl+eANQrz7p/lAzZMnAS8Gg==", 
"post"=>{"title"=>"What I learned today", "content"=>"form_with is a wise child!"}, 
"commit"=>"Post", "exept"=>:index, "controller"=>"posts", "action"=>"create"} permitted: false>

Since it is hard to see, the line breaks at a position that is not originally there, but usually it returns in one line.

" post "=> {" title "=>" What I learned today "," content "=>" form_with is a wise child! "}, Is the value handled by post_params.

If you do not allow the values received by permit, unintended values may be stored in the table, so it is almost mandatory to specify.

** ⑦ Create Post with the value of post_params (save to table if successful) ** Post.create will be done with the value returned by the post_params method, so It is in the same state as below.

  def create
    Post.create(title: "What I learned today", content: "form_with is a wise child!", user_id: 1) 
    redirect_to root_path

After confirming this flow, if you look at the image at the beginning again, you will deepen your understanding!

in conclusion

The differences between form_tag and form_for are summarized in this article. How to write form_with / form_for / form_tag in [Rails] haml

I would appreciate it if you could point out any mistakes.

Recommended Posts

[Rails] Articles for beginners to organize and understand the flow of form_with
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Quickly understand the basics of Java 8 Lambda
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
[Rails] How to get the URL of the transition source and redirect
I want to understand the flow of Spring processing request parameters
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
[Rails] Read the RSS of the site and return the contents to the front
Rails [For beginners] Implementation of comment function
The process of introducing Vuetify to Rails
How to decorate the radio button of rails6 form_with (helper) with CSS
Confirmation and refactoring of the flow from request to controller in [httpclient]
(For beginners) [Rails] Time saving tech! How to install and use slim
[Rails] The cause of not being able to post was in form_with
[Rails] I tried to summarize the passion and functions of the beginners who created the share house search site!
[Rails] Button to return to the top of the page
I tried to organize the session in Rails
Compare the speed of the for statement and the extended for statement.
[For beginners] How to implement the delete function
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
[Introduction to Java] Basics of java arithmetic (for beginners)
[Must-see for beginners] I changed the display of the posting time to Japanese time [l method]
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Library not loaded when trying to upgrade the version of ruby and rails s
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
A review of the code used by rails beginners
How to implement login request processing (Rails / for beginners)
Convert the array of errors.full_messages to characters and output
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
[Rails] How to change the column name of the table
Java classes and instances to understand in the figure
Introduction to Java for beginners Basic knowledge of Java language ①
List of frequently used Java instructions (for beginners and beginners)
[Rails] How to get the contents of strong parameters
Roughly the flow of web application development with Rails.
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Rails] Completely understand form_with
[Ruby On Rails] How to search and save the data of the parent table from the child table
[For beginners] Explanation of classes, instances, and statics in Java
I didn't understand the behavior of Java Scanner and .nextLine ().
Understand the rough flow from request to response in SpringWebMVC
I tried to summarize the basics of kotlin and java
Ruby on Rails for beginners! !! Summary of new posting functions
Command to check the number and status of Java threads
Rails: I've summarized the model and database for a moment.
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Java beginners briefly summarized the behavior of Array and ArrayList
I have summarized the articles that programming beginners referred to.
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Options for rails new and settings to be done after rails new
Tutorial to create a blog with Rails for beginners Part 2
[Rails] Where to be careful in the description of validation
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
Aiming for a basic understanding of the flow of recursive processing
Tutorial to create a blog with Rails for beginners Part 0