得到未定义的方法`错误' for nil:NilClass Ruby on Rails指南

时间:2014-06-18 19:44:33

标签: ruby-on-rails ruby 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>

这是我的追踪:

app / views / articles / edit.html.erb:4:在_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

这是我的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 %>

4 个答案:

答案 0 :(得分:4)

变量定义

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 %>

您需要考虑的其他事项如下:

#app/views/articles/edit.html.erb
<%= form_for @article do |f| %>
   # ...
<% end %>

答案 1 :(得分:3)

是的,你有一个关于实例变量的拼写错误。

<% if @article.errors.any? %>

应该是

<% if @articles.errors.any? %>

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

def edit
    @article = Article.find(params[:id])
end

记住实例变量返回nil,如果您在Ruby中定义它之前尝试使用它。你的情况也是如此。您定义了@articles,但使用了@article,这在您尝试使用之前未由您定义,因此会返回nil。并且nil.errors会在您看到错误时抛出错误。

答案 2 :(得分:3)

问题是@article是nil(null)

在您的控制器中,编辑新操作,如下所示:

def new
  @article = Article.new
end

刷新网页,不再有错误。

答案 3 :(得分:1)

在控制器中编辑新操作

def new

@article = Article.new

因为您的文章实例是nill。