Ruby on rails 视图中的实例变量
我正在按照RubyonRails指南学习RubyonRails的绝对基础知识。然而,我在试图在视图中显示控制器中初始化的变量时遇到了一个问题:在test和show视图中都显示“nil:NilClass的未定义方法”Ruby on rails 视图中的实例变量,ruby-on-rails,ruby,model-view-controller,Ruby On Rails,Ruby,Model View Controller,我正在按照RubyonRails指南学习RubyonRails的绝对基础知识。然而,我在试图在视图中显示控制器中初始化的变量时遇到了一个问题:在test和show视图中都显示“nil:NilClass的未定义方法” app/controller/articles\u controller class ArticlesController < ApplicationController def new end def create @article = Article
app/controller/articles\u controller
class ArticlesController < ApplicationController
def new
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def show
@article = Article.find(params[:id])
end
def test
@testing = [0, 1, 2, 3]
end
end
app/views/articles/new.html.haml
= form_for :article, :url => articles_path do |f|
%p
= f.label :title
%br/
= f.text_field :title
%p
= f.label :text
%br/
= f.text_field :title
%p
= f.submit
app/views/articles/show.html.haml
%p
Title:
%br/
= @article.title
%p
Text:
%br/
= @article.text
app/views/articles/test.html.haml
= @testing[0]
任何帮助都将不胜感激。我不知道我错过了什么。感谢您在控制器中使用@article,在视图中使用@articles。将视图中的@articles更改为@article 另外,将私有方法移到类的底部-显示和测试方法现在在控制器中是私有的
class ArticlesController < ApplicationController
def new
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def show
@article = Article.find(params[:id])
end
def test
@testing = [0, 1, 2, 3]
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
class-ArticlesController
我已经对@文章
做出了回应@testing
似乎还可以-您可以粘贴您得到的确切错误以及您的路由吗?什么不起作用?你有什么错误?粘贴整个错误消息。非常感谢!问题在于方法是私有的
class ArticlesController < ApplicationController
def new
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def show
@article = Article.find(params[:id])
end
def test
@testing = [0, 1, 2, 3]
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end