在ArticlesController#show中的ActiveRecord :: RecordNotFound找不到带有'id'= edit的文章

Sha*_*rra 1 ruby ruby-on-rails rails-activerecord

当我进入文章/编辑时,我收到一个错误.

我收到此错误: 在此输入图像描述

当我应该得到这个错误:

在此输入图像描述

我的代码:

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 
Run Code Online (Sandbox Code Playgroud)

我的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 %>
Run Code Online (Sandbox Code Playgroud)

我的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 %>
Run Code Online (Sandbox Code Playgroud)

我的routes.rb:

resources :articles
root 'pages#home'
get 'about', to: 'pages#about'
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,请向我询问更多文件

EJA*_*JAg 6

那就是错误的原因."编辑"页面需要一个id参数.在您的控制器中:

def edit 
 @article = Article.find(params[:id])
end 
Run Code Online (Sandbox Code Playgroud)

它需要 params[:id]

所以你需要使用/articles/id/edit(用你的实际id替换id)