1
votes

I am doing the tutorial on creating a blogger app from Jumpstart labs http://tutorials.jumpstartlab.com/projects/blogger.html

When i delete articles i a am running into the error "ActiveRecord::RecordNotFound in ArticlesController#show Couldn't find Article with 'id"

The error happens on line 10 of artciles_controller.rb which is @article = Article.find(params[:id])

 articles_controller.rb

class ArticlesController < ApplicationController
    include ArticlesHelper



   def index
       @articles = Article.all
   end
   def edit
     @article = Article.find(params[:id])
   end
   def show
       @article = Article.find(params[:id])
       @comment = Comment.new
       @comment.article_id = @article.id
   end
   def new
       @article = Article.new
   end
   def create
     @article = Article.new(article_params)
     @article.save
     redirect_to article_path(@article)
   end
   def destroy
       @article = Article.find(params[:id])
       @article.destroy
       redirect_to article_path(@article)
   end
   def update
       @article = Article.find(params[:id])
       @article.update(article_params)
       flash.notice = "Article '#{@article.title}' Updated!"
       redirect_to article_path(@article)
   end
end

show.html.erb

<h1><%= @article.title %></h1>
<p><%= @article.body %></p>
<h3>Comments (<%= @article.comments.size %>)</h3>
<%= render partial: 'arti`enter code here`cles/comment', collection: @article.comments %>
<%= link_to "edit", edit_article_path(@article) %>
<%= render partial: 'comments/form' %>
<%= link_to "<< Back to Articles List", articles_path %>
<%= link_to "delete", article_path(@article) , 
        method: :delete , 
     data: {confirm: "Really delete the article?"}%>
2

2 Answers

4
votes
def destroy
    @article = Article.find(params[:id])
    @article.destroy
   redirect_to article_path(@article)
end

If you follow the logic of this code right here, you are destroying the article and then redirecting to the very article you just destroyed. The redirect is working, but because you just destroyed the article it doesn't exist so show cannot find the id. You need to redirect to something else (like the article index, for example).

So replace

redirect_to article_path(@article)

with any other valid redirect (this example will redirect to the articles index)

redirect_to articles_path
1
votes

The error is that you are redirecting to the article_path (the article detail) but that object is destroyed in the previous line, the solution is redirect to the article list, the index method in fact.