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

0 votes
asked Sep 11, 2017 by shashi-yerra

I am getting an error when I got into articles/edit.

I am getting this error: enter image description here

when I should be getting this error:

enter image description here

my code:

articles_controller:

class ArticlesController < ApplicationController

  def new
    @article = Article.new 
  end

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

  def create
    @article = Article.new(article_params)
    if @article.save
      flash[:notice] = "Article was submitted succsefully"
      redirect_to (@article)
    else
      render :new
    end 
  end

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

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

my edit.html.erb :

<h1>Edit the existing article</h1>
<% if @article.errors.any? %>
<h2>The following errors are informing you that if you don't do these then 
your articles will not be edited</h2>
  <ul>
    <% @article.errors.full_messages.each do |msg| %>
      <li> <%= msg %> </li>
    <% end %>
  </ul>
<% end %>
<%= form_for @article do |f| %>
  <p>
    <%= f.label :title %>
    <%= f.text_field:title %>
  </p>
  <p>
    <%= f.label :description  %>
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

my new.html.erb :

<h1>Create an article</h1>
<% if @article.errors.any? %>
<h2>The following errors are informing you that if you don't do these then 
your articles will not be created</h2>
  <ul>
    <% @article.errors.full_messages.each do |msg| %>
      <li> <%= msg %> </li>
    <% end %>
  </ul>
<% end %>
<%= form_for @article do |f| %>
  <p>
    <%= f.label :title %>
    <%= f.text_field:title %>
  </p>
  <p>
    <%= f.label :description  %>
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

my routes.rb:

resources :articles
root 'pages#home'
get 'about', to: 'pages#about'

Ask Me For More Files If It Helps

2 Answers

0 votes
answered Sep 12, 2017 by ej2015

Then that's the cause of the error. 'edit' page requires an id param otherwise. as in your controller:

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

It needs params[:id]

So you need to use /articles/id/edit (replace id with your actual id)

0 votes
answered Sep 12, 2017 by max

resources :articles will create a edit_article_path helper that you should use to link to the edit action:

<%= link_to("Edit article", edit_article_path(@article)) %>

This will create the correct path with a :id segment:

      Prefix Verb   URI Pattern                  Controller#Action
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...