CarlosG90 CarlosG90 - 1 month ago 15
Ruby Question

ActiveRecord::RecordNotFound in ArticlesController#show - Couldn't find Article with 'id'=13

So I'm doing Jumpstarts Blogger tutorial:

http://tutorials.jumpstartlab.com/projects/blogger.html#i1:-form-based-workflow

I'm trying to delete articles by adding a destroy method in my controller, however there seems to be an issue with database when actually deleting the articles. The articles are being deleted when running on my local server, but not from the database it seems?

Here's my articles_controller.rb

class ArticlesController < ApplicationController
include ArticlesHelper

def index
@articles = Article.all
end

def show
@article = Article.find(params[: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
end
end


My show.html.erb

<h1><%= @article.title %></h1>
<p><%= @article.body %></p>
<%= link_to "<< Back to Articles List", articles_path %>
<%= link_to "delete", article_path(@article) ,
method: :delete ,
data: {confirm: "Really delete the article?"}%>


articles.helper.erb

module ArticlesHelper

def article_params
params.require(:article).permit(:title, :body)
end

end


I've also ran the console and searched
Article.find(1)
, and it only finds the index up to 3.

I've been messing around creating and deleting articles (which is why it's up to index 13) and when trying to delete and article I get this error:

ActiveRecord::RecordNotFound in ArticlesController#show

Couldn't find Article with 'id'=13


Any advice or suggestions would be greatly appreciated.

Answer

In your destroy action, change the redirect to articles_path (plural) instead of the singular version. The singular version will attempt to look for the article that you just deleted, which of course no longer exist.

def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to articles_path # pluralized the path
end
Comments