At the beginning of Rails learning, I could not understand the flow of form_with, so I will summarize it. Rails 5.2.4.
I'm sure there are people who say, "Get together quickly!", So I'll summarize the flow first.
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
** ② Pass the @post
instance created by the new action to the view **
post_controller.rb
class PostsController < ApplicationController
~~
def new
@post = Post.new #Generated here@post is new.html.Passed to erb
end
~~
** ③ Enter a value in the instance property and submit **
** ④ 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 **
post_controller.rb
class PostsController < ApplicationController
~~
def create
#The create action is called and Post.create(post_params)Is executed → post_params is called
Post.create(post_params)
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:title, :content).merge(user_id: current_user.id)
end
~~
** ⑥ 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
end
After confirming this flow, if you look at the image at the beginning again, you will deepen your understanding!
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