Assuming the Post – Comment model with the nested resources:
resources :posts do
resources :comments
end
How should the app/views/comments/_form.html.haml (erb will do as well) look like so that it also provides id of the post to attach the comment to?
Current only one way I know is to manually add hidden input with the post id. It looks dirty to me.
Is there any better way? I expected rails to understand the nested resource and automatically include the post_id as a hidden input.
= form_for [@post, @comment] do |f|
.field
f.label :body
f.text_field :body
hidden_field_tag :post_id, @post.id
.actions
= f.submit 'Save'
EDIT: Using Mongoid, not ActiveRecord.
Thanks.
The ID of the post will actually be in the URL. If you type
rake routesinto your terminal/console, you will see that the pattern for your nested resource is defined as such:Take a look at the HTML that is spit out by the
form_formethod, and look specifically at theactionurl of the<form>tag. You should see something likeaction="/posts/4/comments".Assuming that you’ve defined
resources :commentsonly once in yourroutes.rb, as a nested resource ofresources :posts, then it is safe for you to modify theCommentsController#createaction as such:Or you can simply pass
params[:post_id]tocommentinstance like so:I hope this helps.
For more information on nested forms/models, I recommend watching the following Railscasts: