Trouble with understanding params

I don't understand where is :post_id and :comment came from to 'create' action. There is no reference to them in from_for function. http://guides.rubyonrails.org/getting_started.html "7.4 Generating a Controller":

class CommentsController < ApplicationController
 def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.create(params[:comment])
  redirect_to post_path(@post)
 end
end

Upvotes: 0

Views: 122

Answers (1)

xdazz
xdazz

Reputation: 160833

form_for([@post, @post.comments.build]) will produce a form with the action of a url "create a comment for a post", this url has the post_id. And the :comment comes from the form element, which has name attribute like name="comment[commenter]"(which comes from :<%= f.text_field :commenter %>),name="comment[body]"(which comes from: <%= f.text_area :body %>) and so on.

Upvotes: 1

Related Questions