【问题标题】:"undefined method `errors' for nil:NilClass" when calling on errors method调用错误方法时出现“nil:NilClass 的未定义方法‘错误’”
【发布时间】:2013-08-08 21:40:12
【问题描述】:

我目前正在自学一些 RoR 并做教程,但是使用引导程序添加了一些更好的布局和东西,我遇到了一个我无法弄清楚的问题。

我正在尝试进行验证部分 (http://guides.rubyonrails.org/getting_started.html#adding-some-validation),但是当我使用时:

<% @post.errors.any? %>

我收到这条消息:

undefined method `errors' for nil:NilClass
Extracted source (around line #9):
<legend><h1>Add Post</h1></legend>

<%= form_for :post, url: posts_path, html: {class: 'form-horizontal'} do |f| %>
      <% if @post.errors.any? %>
        <div id="errorExplanation">

没有任何效果,我什至复制并粘贴了教程中的部分。

这是视图的代码:

<p> </p>

<div class="span6"

<fieldset>
    <legend><h1>Add Post</h1></legend>

    <%= form_for :post, url: posts_path, html: {class: 'form-horizontal'} do |f| %>
          <% if @post.errors.any? %>
            <div id="errorExplanation">

                <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 %>
        <div class="control-group">
            <%= f.label :title, :class => 'control-label' %>
            <div class="controls">
                <%= f.text_field :title, :class => 'span4' %>
            </div>
        </div>

        <div class="control-group">
            <%= f.label :content, :class => 'control-label' %>
            <div class="controls">
                <%= f.text_area :content, :rows => '7', :class => 'input-block-level' %>
            </div>
        </div>

        <div class="form-actions">
            <%= f.submit "Add Post", :class => 'btn btn-success' %>
            <%= link_to "Cancel", posts_path, :class => 'btn', :style => 'float:right;' %>
        </div>
    <% end %>
</fieldset>

</div>

还有我的posts_controller:

class PostsController < ApplicationController

    def new
    end

    def create
        @post = Post.new(params[:post].permit(:title, :content))

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

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

    def index
        @posts = Post.order("created_at desc")
    end

    private
        def post_params
            params.require(:post).permit(:title, :content)
        end

end

我错过了什么?提前致谢!

【问题讨论】:

    标签: ruby-on-rails erb


    【解决方案1】:

    您还需要在 new 操作中定义 @post

    def new
      @post = Post.new
    end
    

    当您第一次在 new 操作上加载表单时,您会收到 NilClass 错误,因为 @post 没有值(它是 nil

    当您在 create 操作中执行 render :new 时没有问题,因为它使用的是您在 create 顶部定义的 @post

    【讨论】:

    • @Deefour 嗨。我正在做相同的教程并在5.12 Updating Posts 中遇到相同的错误。 @post 变量是在新方法中定义的,它一直抛出这个错误。你能帮帮我吗?
    • 我遇到了同样的问题。这真的很令人困惑,因为教程的上半部分没有明确提到必须在 New 操作中写入 .New。
    【解决方案2】:

    用下面的代码更新posts.controller.rb文件中的create方法。它对我有用。

    def create
      @post = Post.new(params[:post].permit(:title, :text))
      @post.save
      redirect_to @post
    end
    

    【讨论】:

      【解决方案3】:

      在您的 posts_controller 中,添加以下内容:

      def new
        @post = Post.new
      end 
      

      【讨论】:

        【解决方案4】:

        如果您对编辑/创建等进行了更改,但它仍然为您提供 ActionView::Template::Error(nil:NilClass 的未定义方法“错误”):

        尝试重启rails server

        即使在更正了articles_controller.rb 中的文件后,我仍然收到错误消息

        仍然出现错误。在我重新启动 Rails 服务器后它已修复。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-27
          • 2015-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-13
          • 2020-04-21
          相关资源
          最近更新 更多