【问题标题】:Why do I get ActionView::Template::Error: undefined method `isebook' for nil:NilClass on Heroku but not locally为什么我在 Heroku 上得到 ActionView::Template::Error: undefined method `isebook' for nil:NilClass 但不是在本地
【发布时间】:2014-11-10 17:21:24
【问题描述】:

我有一个 Rails 4 应用程序在本地运行 SQLite,在 Heroku 上运行 PostgreSQL。

我有一个产品类,它有一个布尔列“isebook”,在 products/new.html.erb 的表单中设置为 true 或 false。

<%= form_for(@product, :html => {:multipart => true}) do |f| %>
      <% if @product.errors.any? %>
        <div class="alert alert-danger alert-dismissable">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>

          <h3><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h3>

          <ul>
          <% @product.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <div class="form-group">
        <%= f.label :name %><br>
        <%= f.text_field :name, class: "form-control"  %>
      </div>
      <div class="form-group">
        <%= f.label :link %><br>
        <%= f.text_field :link, class: "form-control"  %>
        <p style="font-size:12px; margin-top:2px;"><i>(Begin link with http:// or https://)</i></p>
      </div>
      <div class="form-group">
        <%= f.label :description %><br>
        <%= f.text_area :description, class: "form-control"  %>
      </div>
      <div class="form-group">
        <%= f.label "Product is an Ebook?" %> <br />
        <%= f.check_box :isebook %> <br />
      </div>
      <div class="form-group">
        <%= f.label :image, 'Product Image'  %><br>
        <%= f.file_field :image, class: "form-control"  %>

      </div>
      <div class="actions">
        <%= f.submit "Create Product", class: "btn btn-primary"  %>
      </div>
    <% end %>

在控制器中,此字段是必需的:

def product_params
  params.require(:product).permit(:name, :description, :isebook, :image)
end

控制器方法的完整代码:

def new

# Only make new product if user has remaining products

@product = Product.new

if current_user.prodused < current_user.prodlimit
  respond_with(@product)
else
  redirect_to user_path(current_user.id), error: "You've reached your product limit. Upgrade your account to add more products."
end

end

这是产品型号:

class Product < ActiveRecord::Base

 validates :name, :description, presence:true
 validates :link, :format => URI::regexp(%w(http https))

 belongs_to :user

 has_attached_file :image, :styles => {:medium => "300x300", :thumb => "200x200"}, :default_url => "/images/:style/missing.jpg"

end

这在本地可以正常工作,当创建新产品时,isebook 字段会根据是否选中复选框来分配 true 或 false。

我现在正在尝试在 Heroku 上进行测试,我清除了整个生产数据库并运行了所有迁移文件以清除所有现有产品。然后我成功创建了一个帐户,在尝试呈现 products/new.html.erb 时出现错误 ActionView::Template::Error: undefined method `isebook' for nil:NilClas

【问题讨论】:

  • 贴出整个表单的代码,以及对应的控制器动作
  • 编辑添加整个表单代码,以及相应的控制器操作。
  • 从堆栈跟踪中你能找出导致问题的行吗?代码看起来不错。
  • 我不熟悉堆栈跟踪,但是当我使用设置 isebook 字段的复选框注释掉 div 元素时,不再发生错误

标签: ruby-on-rails heroku


【解决方案1】:

我清除了整个生产数据库并运行了所有迁移文件以清除所有现有产品。然后我成功创建了一个帐户

有产品吗?

ActionView::Template::Error: undefined method `isebook' for nil:NilClas

此错误表示您在nil 上调用了方法isebook,可能意味着您尝试加载一个或所有产品,但在您的情况下它们不存在,因此在nil 上调用该方法(什么都没有)抛出一个错误。

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多