This example shows how to create a form for an associated model ‘Comment’, where ‘Comment’ belongs_to ‘Post’ and ‘Post has_many ‘Comments’.
http://edgeguides.rubyonrails.org/getting_started.html#generating-a-controller
How might I modify that to display all the comments in the form, and still have it call the CommentsController (versus having the form call the PostsController, as in the example here http://railscasts.com/episodes/17-habtm-checkboxes)?
Thanks
UPDATE:
To ask a different way: Using a Polymorphic Assocation, if Photo and Article each ‘has_many’ Comments, the comment form should call the CommentsController, as shown here http://railscasts.com/episodes/154-polymorphic-association — But what if I’m editing multiple comments in one form for a given Photo. In this case should the form still call the CommentsController or is it better off calling the PhotoController?
Here’s the updated answer:
I’d still use the
CommentsControllerto do all the updating of comments. Unless you’re editing the post and its comments at the same time. Since you’re editing a post’s comments, what you can do is justPOSTthem to an action in the comments controller. In your view, make a form with afields_forfor each comment.Then, they can all post to an
update_multiple(or something similarly named) action in theCommentsController. It’s just a matter of accepting the params hash and parsing it properly.In your model (just make it more robust by doing it all in a transaction, so if one fails, then none of them are updated. Well, that’s really up to you):