Ruby on rails 获取未定义的方法“errors';for nil:NilClass Ruby on Rails指南
好的,我一直在寻找一个和我的问题完全相同的问题,但我找不到,所以我不得不自己问。我在这里遵循指南: 我在编辑文章页面上遇到了这样一个错误:“nil:NilClass的未定义方法'errors'” 以下是我提取的源代码:Ruby on rails 获取未定义的方法“errors';for nil:NilClass Ruby on Rails指南,ruby-on-rails,ruby,ruby-on-rails-3,railstutorial.org,Ruby On Rails,Ruby,Ruby On Rails 3,Railstutorial.org,好的,我一直在寻找一个和我的问题完全相同的问题,但我找不到,所以我不得不自己问。我在这里遵循指南: 我在编辑文章页面上遇到了这样一个错误:“nil:NilClass的未定义方法'errors'” 以下是我提取的源代码: <h1>Editing article</h1> <%= form_for :article, url: articles_path(@article), method: :patch do |f| %> <% if @arti
<h1>Editing article</h1>
<%= form_for :article, url: articles_path(@article), method: :patch do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited
this article from being saved: </h2>
编辑文章
禁止
要保存此文章,请执行以下操作:
这是我的追踪:
app/views/articles/edit.html.erb:4:in-block-in\u-app\u-views\u-articles\u-edit\u-html\u-erb\u 778692675\u\u 618464548
app/views/articles/edit.html.erb:3:in_app_views_articles_edit_html_erb_778692675_618464548
这是我的动作控制器
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def edit
@articles = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def show
@article = Article.find(params[:id])
end
def index
@articles = Article.all
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
class-ArticlesController
这是我的edit.html.erb
<h1>Editing article</h1>
<%= form_for :article, url: articles_path(@article), method: :patch do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited this article
from being saved: </h2>
<ul>
<% @article.errors.full_messages.each do |msg| %><li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
编辑文章
禁止这篇文章
从被保存开始:
是的,实例变量输入错误
<% if @article.errors.any? %>
记住,如果在Ruby中定义实例变量之前尝试使用它,那么实例变量将返回
nil
。你的情况也是如此。您定义了@articles
,但使用了@article
,在尝试使用它之前您没有定义它,因此返回nil
。并且nil.errors
抛出您看到的错误。变量定义
<> >您可能想考虑的是,除了<代码>阿勒普<代码>的答案,错误本身是:
“nil:NilClass的未定义方法'errors'”
返回的是异常,因为您试图调用nil
类对象上的方法。正如奥雅纳所指出的,这通常是由于您在没有首先定义它的情况下调用了@instance\u变量
我想强调一个事实,您的错误表明问题在于您的对象有一个未定义的方法。大多数人会将问题视为由于某种原因而未定义方法;实际上,问题是您没有定义对象
--
修复
正如Arup指出的,修复错误的方法是引用编辑方法中定义的@实例变量
,如下所示:
#app/controllers/articles_controller.rb
Class ArticlesController < ApplicationController
def edit
@article = Article.find params[:id]
end
end
#app/views/articles/edit.html.erb
<%= @article.errors %>
def new
@article = Article.new
end
#app/controllers/articles_controller.rb
类ArticlesController<应用程序控制器
定义编辑
@article=article.find参数[:id]
结束
结束
#app/views/articles/edit.html.erb
<>你想考虑的其他事情是:
#app/views/articles/edit.html.erb
<%= form_for @article do |f| %>
# ...
<% end %>
#app/views/articles/edit.html.erb
# ...
在控制器中编辑新操作
def新
@article=article.new
结束
因为您的文章实例是nill。问题在于@article是nil(null)
在控制器中,编辑新操作,使其如下所示:
#app/controllers/articles_controller.rb
Class ArticlesController < ApplicationController
def edit
@article = Article.find params[:id]
end
end
#app/views/articles/edit.html.erb
<%= @article.errors %>
def new
@article = Article.new
end
刷新网页,不会再有错误。但它应该命名为@article
,因为它是一篇文章。