Search code examples
rubyruby-on-rails-4

Couldn't find Article with 'id'=show


I know this question may be simple and related to other problems but in my case those solutions are not working, so my question is how to deal with this problem?

While I try to show the article I am getting this error:

ActiveRecord::RecordNotFound in ArticlesController#show couldn't find article with 'id'=show

I am stucking here

@article = Article.find(params[:id])

Here is my code snippets My article_controller.rb look like this

class ArticlesController < ApplicationController
  def index
    @articles= Article.all
  end

  def show
    @article = Article.find(params[:id])
  end

  def new
    @article= current_user.articles.build
  end

  def create
    @article=current_user.articles.build(@article_params)
    if @article.save
      redirect_to articles_path
    else
      render 'new'
    end
  end

  def edit
    @article=Article.new(article_params)
  end 
end

my index.html.erb looks like this

<h2>welcome index</h2>

<ul>
  <% @articles.each do |article| %>
    <%= article.titile %>
    <%= article.body %>
  <% end %>
</ul>

my show.html.erb look like this

<h2><%= @article.title %></h2>
<p><%= @article.body %></p>

my Routes look like this

Rails.application.routes.draw do
  devise_for :users

  root 'articles#index'

  resources :articles

  get 'articles/show'
end

Solution

  • Remove get 'articles/show' line from your routes.rb file.