【问题标题】:Getting undefined method `errors' for nil:NilClass on the view new在新视图上获取 nil:NilClass 的未定义方法“错误”
【发布时间】:2015-11-05 16:38:21
【问题描述】:

我是 ruby​​ 新手,我正在学习,所以我一直在学习这个教程 http://guides.rubyonrails.org/getting_started.html 但是当我尝试添加错误消息时,我得到 nil:NilClass 的未定义方法“错误” 在这段代码中:

<h1>New Article</h1>
     <%= form_for :posts, url: posts_path do |f| %>
      <% if @post.errors.any? %> // <-- **HERE**
          <div id="error_explanation">
            <h2>
              <%= pluralize(@post.errors.count, "error") %> prohibited

这是我的控制器:

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def new
    @posts = Post.all
  end

  def create
      @posts = Post.new(posts_params)
    if @posts.save
        redirect_to @posts
    else
        render 'new'
    end
  end

  def show
    @posts = Post.find(params[:id])
  end

  private
  def posts_params
    params.require(:posts).permit(:title, :description)
  end  
end

这是我的观点,它给了我一个完整的错误:

<h1>New Article</h1>
<%= form_for :posts, url: posts_path do |f| %>
<% if @post.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@post.errors.count, "error") %> prohibited
        this post from being saved:
      </h2>
      <ul>
        <% @post.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 :description %><br>
    <%= f.text_area :description %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
<%= link_to 'Back', posts_path %>

My model: 



 class Post < ActiveRecord::Base

        validates :title, 
                :presence => {:message => "Title can't be blank." },
                :uniqueness => {:message => "Title already exists."},
                 length: { minimum: 5 }

        validates :description, 
        :presence =>{:message => "Description can't be blank." }
    end

有人可以帮我吗?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    你有以下:

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

    但在你的控制器中,你有:

     def new
        @posts = Post.new
      end
    
     def create
        @posts = Post.new(posts_params)
        ...
     end
    

    在您的控制器中将 @posts 重命名为 @post

    因此,代码必须如下所示:

    def new
        @post = Post.new
      end
    
     def create
        @post = Post.new(posts_params)
        ...
     end
    

    【讨论】:

    • 我现在有这个类 PostsController 的未定义方法“错误”刚刚将@posts 更改为@post,正如你所说
    • 对不起,我没有先注意到它。将 Post.all 更改为 Post.new
    • 哦,天哪,非常感谢,这完全有效,我之前尝试过,但只更改了其中一个,现在一切正常:D
    • 遇到另一个问题 :T 索引视图:

      Listing posts

      Title Text
      得到这个错误 undefined method `each' for #
    • 在@posts.each 做|posts|
    【解决方案2】:

    我想建议,只有一件事:-

    • 由于您是 ruby​​ 新手,请先尝试传统方法获取验证错误并根据验证结果显示它,然后您可以继续试验原因和其他方法来找到根本原因。

    你可以试试:-

    In controller
    
    def new
     @post = Post.new
    end 
    

    在你的视野中

    <%= form_for :post, url: posts_path do |f| %>
     <% if @post.errors.any? %>
     ---you code here --
     <% end %>
    <% end %>
    

    new 操作可用于在保存到数据库之前验证参数,因此如果未提供有效参数,则调用 some_obj = obj.new(params) 然后som_obj.errors 肯定会列出错误

    【讨论】:

      【解决方案3】:

      错误是你没有声明@post

      --

      您需要执行以下操作:

      #app/controllers/posts_controller.rb
      class PostsController < ApplicationController
         def new
             @post = Post.new
         end
      end
      
      #app/views/posts/new.html.erb
      <%= form_for @post do |f| %>
         <% if @post.errors.any? %>
            ...
         <% end %>
         <%= f.submit %>
      <% end %>
      

      这将设置正确的@post 变量,这应该使您能够在其上调用.errors 方法。


      作为旁注,

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

      这个错误经常与新开发者混淆。

      他们认为他们的问题是errors 方法丢失; 真正的问题是您的变量未声明

      由于Ruby is object orientated,它将每个变量视为一个对象。与其他语言不同 - 会根据未声明的变量引发异常,Ruby 填充 nil:NilClass 对象。

      因此,每当您看到上述错误时,请始终记住这意味着您调用了一个未声明的变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        • 2012-12-27
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多