我正在使用本指南here
我一直试图在ArticlesController #index中解决问题NameError 当我开始将评论添加为资源,控制器等时,就会出现这种情况。
ArticlesController #index中的NameError 告诉我 app / models / article.rb:1:in
<top (required)>'
app/controllers/articles_controller.rb:3:in index'
这是我的代码:
class ArticlesController < ApplicationController
def index
@article = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
这段代码:
class Article < ApplicationRecord
has_many :comments
validates :title, presence: true,
length: { minimum: 5 }
end
我是否拥有问题中所需的所有代码?
答案 0 :(得分:-1)
查看错误<top (required)>'app/controllers/articles_controller.rb:3:inindex'
,我认为问题来自您的index action
应该是
def index
@articles = Article.all
end
也将模型更改为
class Article < ActiveRecord::Base
has_many :comments
validates :title, presence: true, length: { minimum: 5 }
end
因为您的Article
模型应该继承自ActiveRecord::Base
而不是ApplicationRecord