【问题标题】:undefined method `errors' for nil:NilClass in app/views/articles/new.html.erbnil 的未定义方法“错误”:app/views/articles/new.html.erb 中的 NilClass
【发布时间】:2015-05-13 14:02:17
【问题描述】:

我正在关注http://guides.rubyonrails.org/getting_started.html 并尝试向文本字段添加验证。我的班级:

class Article < ActiveRecord::Base

    validates :title, presence: true, length: { minimum: 5 }

    def new
      @article = Article.new
    end

    def create
    @article = Article.new(article_params)

      if @article.save
        redirect_to @article
      else
        render 'new'
        end
    end

    private
    def article_params
        params.require(:article).permit(:title, :text)
    end

end

我的 new.html.erb 文件:

<%= form_for :article, url: articles_path 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 %>

当我尝试添加新文章时,我打开http://localhost:3000/articles/new,但我看到的不是表单,而是undefined method errors' for nil:NilClass,因为&lt;% if @article.errors.any? %&gt; 这一行中的错误

我在这里错过了什么。看起来@article 在创建之前正在被验证?我该如何解决?

【问题讨论】:

  • 你可以用form_for @article代替form_for :article, url: articles_path
  • @SergioTulentsev 代码是在 Rails 指南中写的,只是复制粘贴。
  • 看来您将代码复制粘贴到错误的位置,我认为指南没有问题。甚至在这段代码之前它说“打开app/models/article.rb文件并编辑它:”

标签: ruby-on-rails ruby


【解决方案1】:

您的模型和控制器都被合并到一个类中。那是行不通的。

你需要两个类:

  • 一个名为Article的模型继承自ActiveRecord::Base
  • 一个名为ArticlesController 的控制器继承自ApplicationController

您发布的代码是模型,但您添加的操作(newcreate)需要进入 控制器

您所遵循的指南说(注意文件名):

Rails 包含可帮助您验证发送到模型的数据的方法。打开 app/models/article.rb 文件并进行编辑:

class Article < ActiveRecord::Base
    validates :title, presence: true,
             length: { minimum: 5 }
end

还有下面的,

...为此,将 app/controllers/articles_controller.rb 中的新操作和创建操作更改为:

def new
  @article = Article.new
end

def create
  @article = Article.new(article_params)

  # ...

【讨论】:

  • 打败我 - 我难以置信地盯着模型,因为我对 Rails 很陌生,所以我想在回答同样的问题之前检查我自己的代码,哈哈
  • 多么愚蠢的错误!我使用模型类而不是控制器。天啊。当然,我把代码移到了正确的地方,现在就好了。
【解决方案2】:

验证部分应该驻留在 Modal 中,即 Article

【讨论】:

    【解决方案3】:

    你必须把你的验证放在你的模型中:

    class Article < ActiveRecord::Base
      validates :title, presence: true, length: { minimum: 5 }
    end
    

    你的动作应该在控制器中:

    class ArticlesController < ApplicationController
    
      def new
        @article = Article.new
      end
    
      def create
        @article = Article.new(article_params)
        if @article.save
           redirect_to @article
        else
           render 'new'
        end
      end
    
      private
       def article_params
         params.require(:article).permit(:title, :text)
       end
    
    
    end
    

    最后改变你的形式:

    <%= form_for @article do |f| %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多