Reputation: 307
I'm currently following along the basic Rails guide for creating a blog and I'm having trouble with deleting my comments
associated with the post.
Here is my comments controller:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end
def destroy
@post = Post.find params[:post_id]
@comment = @post.comments.find params[:id]
@comment.destroy
redirect_to post_path(@post)
end
end
In my views, the destroy
action isn't firing at all. So I can't even use the console as a tool to solve the problem.
Here is the views partial for comments
:
h2 Comments
- @post.comments.each do |comment|
p
strong Commenter:
= comment.commenter
p
strong Comment:
= comment.body
p = link_to "Delete Comment", [comment.post, comment],
method: :delete,
data: { confirm: 'Are you sure?' }
Generated html for the delete link:
<a data-confirm="Are you sure?" data-method="delete" href="/posts/9/comments/2"
rel="nofollow">Delete Comment</a>
Even my destroy action for posts is not making any requests, which makes me think the problem mightve been when I moved to partials.
Inside the console I can still delete a comment by finding the post by its post_id
, then finding the comment associated with the post and destroying it. For some reason I can't figure out why the views link won't do anything.
Upvotes: 0
Views: 2917
Reputation: 1
finally, you loop on these comments and destroy them.
@post = Post.find(params[:id])
@comment = @post.comments.where(post_id: @post.id)
@comment.each do |comment|
comment.destroy
end
Upvotes: 0
Reputation: 5611
Please, first of all be sure to place the correct indentation in your template, this may mess up things. Te last paragraph with the anchor link seems to be mistyped:
h2 Comments
- @post.comments.each do |comment|
p
strong Commenter:
= comment.commenter
p
strong Comment:
= comment.body
p
= link_to "Delete Comment", [comment.post, comment], method: :delete, data: { confirm: 'Are you sure?' }
Then, you should look at the rails server logs to see what happens when you click on the link. If the HTML link looks fine, a call to the server must be there, with all the information you need to debug the call.
Upvotes: 4