{"id":1258,"date":"2022-08-30T15:15:01","date_gmt":"2022-08-30T15:15:01","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/trouble-with-deleting-my-blog-posts-using-the-ui-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:01","modified_gmt":"2022-08-30T15:15:01","slug":"trouble-with-deleting-my-blog-posts-using-the-ui-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/trouble-with-deleting-my-blog-posts-using-the-ui-collection-of-common-programming-errors\/","title":{"rendered":"Trouble with deleting my blog posts using the UI-Collection of common programming errors"},"content":{"rendered":"<p>I am very new to Ruby on Rails so apologies, as this may be a silly question to post here.<\/p>\n<p>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 (&#8220;index&#8221;), an area to view a specific blog post (&#8220;show&#8221;), and area to edit a blog post (&#8220;edit&#8221;), and an area to create a new blog post (&#8220;new&#8221;). I&#8217;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 &#8220;show&#8221; page.<\/p>\n<p>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 &#8220;index&#8221; page aren&#8217;t working. Instead of prompting confirmation for the delete, I&#8217;m simply taken to the &#8220;show&#8221; of the relevant post.<\/p>\n<p>Here is a snippet of the index &#8220;index&#8221; page:<\/p>\n<pre><code>\n<\/code><\/pre>\n<tr>\n<td><\/td>\n<td><\/td>\n<td>&#8221; %&gt;<\/td>\n<td>ago ()<\/td>\n<td class=\"zero-out\">&#8220;submit-button&#8221; %&gt;<\/td>\n<td class=\"zero-out\">&#8220;submit-button&#8221; %&gt;<\/td>\n<td class=\"zero-out\">:delete, :class =&gt; &#8220;submit-button&#8221; %&gt;<\/td>\n<\/tr>\n<p>And here is the snippet of code from the posts_controller relevant to the delete:<\/p>\n<pre><code>def destroy\n@post = Post.find(params[:id])\n@post.destroy\n\nrespond_to do |format|\n  format.html { redirect_to posts_url }\n  format.json { head :ok }\nend\nend\n<\/code><\/pre>\n<p>I have also found that the remove comment buttons (on the &#8220;show&#8221; page alongside each comment) have stopped working with an error message:<\/p>\n<blockquote>\n<p>Unknown action<\/p>\n<p>The action &#8216;show&#8217; could not be found for CommentsController<\/p>\n<\/blockquote>\n<p>For reference the code for the &#8220;remove comment&#8221; button is:<\/p>\n<pre><code> 'Are you sure?',\n    :method =&gt; :delete %&gt;\n<\/code><\/pre>\n<p>And the snippet of code in the comments_controller is:<\/p>\n<pre><code>def destroy\n@post = Post.find(params[:post_id])\n@comment = @post.comments.find(params[:id])\n@comment.destroy\nredirect_to post_path(@post)\nend\n<\/code><\/pre>\n<p>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.<\/p>\n<ol>\n<li>\n<p>I&#8217;ve found the problem &#8211; 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.<\/p>\n<p>After including links to these files everything worked again.<\/p>\n<\/li>\n<li>\n<p>First you miss the arrow in the destroy link method call Correct it to this.<\/p>\n<pre><code> :delete, :class =&gt; \"submit-button\" %&gt;\n<\/code><\/pre>\n<p>Second you don&#8217;t have to find the post of a comment to delete it simply do this.<\/p>\n<p>If you want to be redirected back to the post, you do it in the controller<\/p>\n<pre><code> :delete %&gt;\n<\/code><\/pre>\n<p>in the comment_controller<\/p>\n<pre><code>def destroy\n    @comment = Comment.find(params[:id])\n    @post = @comment.post\n    @comment.destroy\n\n    respond_to do |format|\n      format.html { redirect_to @post }\n      format.json { head :ok }\n    end\n  end\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:12:37. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 (&#8220;index&#8221;), an area to view a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1258","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1258"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1258\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}