ruby-on-rails - 调用错误方法时出现 "undefined method ` 错误 ' for nil:NilClass"

标签 ruby-on-rails erb

我目前正在自学一些 RoR 并编写教程,但是使用 bootstrap 添加一些更好的布局和内容,我遇到了一个我无法解决的问题。

我正在尝试执行验证部分( 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">

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

这是 View 的代码:

<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

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

最佳答案

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

def new
  @post = Post.new
end

您收到 NilClass 错误,因为 @post 没有值(它是 nil)首先在 new 操作上加载表单。

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

关于ruby-on-rails - 调用错误方法时出现 "undefined method ` 错误 ' for nil:NilClass",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18136632/

相关文章:

mysql - 获取分页查询中的结果总数

ruby - 如何在 erb 中为 2 个布局创建 1 个 View ?

jquery - 在 AJAX 调用 Rails 应用程序后使用 js.erb 渲染页面后,HTML 类不响应 jQuery 操作

html - Ubuntu 服务器 14.04 上的 Rails,未加载应用程序布局

javascript - 如何在 erb 中安全地呈现 URI

ruby-on-rails - 如何将特定的 gem 版本设置为默认版本?

ruby-on-rails - Webrat (web_steps.rb) 未被使用

ruby-on-rails - 将 Devise 用于两种不同的型号但相同的登录表单

ruby-on-rails - 在我对寄存器文件进行任何更改之前,关联的(动态)范围不起作用

ruby - 在 Sinatra 1.2.0 和 Ruby 1.9.2 上使用 Case Expression 的 SyntaxError