Rails: How to initialize instance variable in new action -
i have 2 models post , comments.
class post < activerecord::base has_many :comments end class comment < activerecord::base belongs_to :post end
my form creating new comment below
<%= form_for @comment , :url => post_comments_path(params[:post_id]) |f| %> <%= f.text_area :title %> <%= f.submit "add comment" %> <% end %>
i having confusion new action of above form. in new action can initialize @comment instance variable below 2 ways.
@comment = comment.new or @post = post.find(params[:id) @comment = @post.comments.build(set_params)
my question difference between comment.new , @post.comments.build(set_params).
the latter preferred, because set post_id
on comment.
Comments
Post a Comment