尝试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
答案 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