Trouble with deleting my blog posts using the UI-Collection of common programming errors
I am very new to Ruby on Rails so apologies, as this may be a silly question to post here.
I have made a blog (using generate scaffold). I have a few pages to interact and edit blog posts, starting with a main page which displays all blog posts (“index”), an area to view a specific blog post (“show”), and area to edit a blog post (“edit”), and an area to create a new blog post (“new”). I’ve also created comments (again using generate scaffold) to be applied to relevant blog posts. Comments, and a partial form for comments appears on the “show” page.
I have been working away on the whole thing to get it working nicely, but have recently realised that delete buttons that I had on the “index” page aren’t working. Instead of prompting confirmation for the delete, I’m simply taken to the “show” of the relevant post.
Here is a snippet of the index “index” page:
And here is the snippet of code from the posts_controller relevant to the delete:
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :ok }
end
end
I have also found that the remove comment buttons (on the “show” page alongside each comment) have stopped working with an error message:
Unknown action
The action ‘show’ could not be found for CommentsController
For reference the code for the “remove comment” button is:
'Are you sure?',
:method => :delete %>
And the snippet of code in the comments_controller is:
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
I lack the full knowledge of how RoR works, and how these files interact with each other fully to troubleshoot the problem, so any help would be very much appreciated.
-
I’ve found the problem – I had customised the links to stylesheets, javascript files etc and in doing this I had left out links to the javascript files for posts and comments created by the scaffold.
After including links to these files everything worked again.
-
First you miss the arrow in the destroy link method call Correct it to this.
:delete, :class => "submit-button" %>
Second you don’t have to find the post of a comment to delete it simply do this.
If you want to be redirected back to the post, you do it in the controller
:delete %>
in the comment_controller
def destroy @comment = Comment.find(params[:id]) @post = @comment.post @comment.destroy respond_to do |format| format.html { redirect_to @post } format.json { head :ok } end end
Originally posted 2013-11-10 00:12:37.