Experiences on the Front Lines of User Interfaces and Web Development

Rails FormHelper with model relationships and has_many route

Using Rails 2.3.5

To do things RESTfully in Rails and you have a model relationship like:

You can set up your routes like this:

This will generate URLs like: {gfm-js-extract-pre-3}
Then to construct your form for creating or editing comments, you can do this with form_for: {gfm-js-extract-pre-4}

Then in your CommentsController::update action, you will automatically have params[:post_id] so that comments are associated with the correct post

By the way, if you need to create those restful URLs by hand using url_for, this is how you would specify the create one: {gfm-js-extract-pre-5}
Notice that the controller is 'comments', even though the URL created with start with /posts

see form_for (ActionView::Helpers::FormHelper) - APIdock

comments powered by Disqus