I have a model Post that has_many :comments. The form that will post the comment will be shown along with the post in posts/show.html.erb. I have a comments_controller that should handle the creation of comments. Searching on google, I found
<%= form_for([@post, Comment.new], :controller => 'comments', :action => 'create') do |f| %>
But this doesn’t work. How do I do this ?
Then in the form
this would create the associated object through active record’s accepts_nested_attributes_for, which doesn’t require a separate comments_controller. you are submitting to the posts controller, which is handling creating the associated object during the update of the post.
with a comments_controller, you could do one of two things:
send item_id as a param to comments_controller#new, grab the item, then build the new comment from it
put the post_id in a hidden field on the form and just create the comment as normal