Rapid Prototyping with Rails: Lesson 2, Nested Routes
20 Oct 2014Comments - using Nested Routes
def create
@post = Post.find(params[:post_id])
@comment = Comment.new(params.require(:comment).permit(:body))
@comment.post = @post
end
OR
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(params.require(:comment).permit(:body))
end
What we've covered:
Creating a Nested Resource
Creating a model-backed form in a nested resource way... to generate a nested URL
In your controller Create action, you must set up the parent object in the controller action, so that it can be called on in your form_for
Under the error condition, you have to render 'posts/show' rather than 'new'