In my rails website I want to make a form with nested resources. More specificly, I want to add a comment section to my blogposts.
The solution I found on several places is creating a form with the rails helper form_for and binding both the @post instance variable and the @comment instance variable to the form like this
<%= form_for[@post, @comment] do |f| %>
Now I was hoping that someone could explain me why it doesn't work to just bind it to @comment, even if you that comment has a foreign key to @post?
So in other words I still need to bind the form to both instance variables, even if I defined the following in my show action.
@post = Post.find(params[:id])
@comment = @post.comments.new
Rails is pretty smart, why isn't it able to figure out that @comment belongs to @post?
form_for @comment...in your view and you'll get back comment attribute values which you can use to create acommentinstance which is bound to the rightpost.undefined method 'comments_path'when I don't bind the form to @post