为 nil 获取未定义的方法“错误”:NilClass Ruby on Rails 指南

lgd*_*ruz 5 ruby ruby-on-rails ruby-on-rails-3 railstutorial.org

好吧,我一直在这里寻找一个与我的完全相同的问题,但我找不到,所以我不得不自己问这个问题。我正在关注这里的指南:http : //guides.rubyonrails.org/getting_started.html

我遇到了这个错误,说:“未定义的方法‘错误’为 nil:NilClass”为我的编辑文章页面。

这是我提取的来源:

 <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>
Run Code Online (Sandbox Code Playgroud)

这是我的踪迹:

app/views/articles/edit.html.erb:4:in block in _app_views_articles_edit_html_erb___778692675__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
Run Code Online (Sandbox Code Playgroud)

这是我的 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 %>
Run Code Online (Sandbox Code Playgroud)

Ric*_*eck 5

变量定义

除了Arup的答案之外,您可能还想考虑的是错误本身:

“nil:NilClass 的未定义方法‘错误’”

你得到的是一个异常,因为你试图调用一个nil类对象的方法。正如Arup指出的那样,这通常是由于您在@instance_variable没有先定义它的情况下调用了

我想强调一个事实,即您的错误表明问题在于您的对象有一个未定义的方法。大多数人会将问题视为您的方法由于某种原因未定义;实际上,问题是您没有定义对象。

——

使固定

正如Arup指出的那样,修复错误的方法是引用方法中@instance variable定义的edit,如下所示:

#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 %>
Run Code Online (Sandbox Code Playgroud)

您还需要考虑以下事项:

#app/views/articles/edit.html.erb
<%= form_for @article do |f| %>
   # ...
<% end %>
Run Code Online (Sandbox Code Playgroud)


Aru*_*hit 3

是的,您的实例变量有一个拼写错误。

<% if @article.errors.any? %>
Run Code Online (Sandbox Code Playgroud)

应该

<% if @articles.errors.any? %>
Run Code Online (Sandbox Code Playgroud)

因为在控制器操作内部#edit,您已经定义了@articlesnot @article。但它应该被命名,@article因为它是一篇文章。因此保持<% if @article.errors.any? %>原样,将#edit方法更改为

def edit
    @article = Article.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)

如果您在 Ruby 中定义实例变量之前尝试使用它,请记住实例变量returns 。nil你的情况也发生了同样的情况。您定义了@articles,但使用了@article,而您在尝试使用它之前并未定义它,因此返回nil。并nil.errors抛出错误,如您所见。

  • 但它*应该*命名为“@article”,因为它是一篇文章。 (3认同)