在ArticlesController#edit中的ActiveRecord :: RecordNotFound

时间:2016-04-18 22:57:21

标签: ruby-on-rails activerecord

尝试edit Article时收到错误消息。这是我的错误:

ActiveRecord::RecordNotFound in ArticlesController#edit

articles.controller.rb

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 successfully created"
     redirect_to article_path(@article)
   else
     render 'new'
   end
  end

 def update
  @article = Article.find(params[:id])
  if @article.update
    flash[:notice] = "Article was successfully updated"
    redirect_to article_path(@article)
  else
    render 'edit'
  end
 end

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

1 个答案:

答案 0 :(得分:1)

您需要将@article传递到网址中。你应该有一个类似这样的网址:

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

另外请务必检查您的数据库并确保Article实际上正在保存。我没有看到您的article_params,但我认为它们的定义是您在create方法中使用它们。

您的 routes.rb 看起来像这样:

Rails.application.routes.draw do
  resources :articles
end