Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Our blog app now lets us create, read, and update Comments that belong to a Post. Lastly, we need to support deleting Comments.
Our blog app now lets us create, read, and update Comments that belong to a Post. Lastly, we need to support deleting Comments.
- We can set up a delete link in the
_comment.html.erb
partial:
views/comments/_comment.html.erb
<div class="comment-admin">
<%= link_to "Edit", edit_post_comment_path(@post, comment) %>
<!-- NEW CODE BELOW -->
<%= link_to "Delete", [@post, comment], method: :delete %>
</div>
- We need a
destroy
action on theCommentsController
:
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to @post
end
Further Reading
Rails Guides: Nested Resources
Extra Credit
We currently have a before_action
named set_post
on the CommentsController
. Try adding a second before_action
named set_comment
that sets the @comment
instance variable.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up