I understand that the new
action for a rails form normally instantiates a model and provides a view with inputs for permitted params.
def new
@post = Post.new
end
What I don't get is how that same information submitted by the user is carried over to Post.new
method inside the create
action:
def create
@post = Post.new(post_params)
...
end
How does rails channel these params to create
despite the page refresh that occurs?
You are mixing two requests with each other. When you hit the new
action, a view that contains your form
gets rendered. As it gets rendered, the new
action finishes, and now the new
action has nothing to do with create
action.
You need new
action to create a form
through form_for
method in which you actually pass an object, in your case @post
.
The create
action is independent, and params
it receives, they have nothing to do with new
method, those params
are received through form
rendered in the new.html.erb file of views.
You can also invoke the create
method in your controller by sending the data through AJAX or even using cURL or POSTMAN - a chrome extension.
And as you asked:
How does rails channel these params to create despite the page refresh that occurs?
Rails doesn't channel these params
, Rails run at back-end, Rails just receives those params
. They are sent through an HTML form, and as I said earlier there are other ways to send params
as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.