Ruby on rails 为什么要获取ActionView::Template::Error:未定义的方法`isebook';for nil:Heroku上的NilClass,但不是本地的
我有一个Rails 4应用程序在本地运行SQLite,在Heroku上运行PostgreSQL 我有一个products类,它有一个布尔列“isebook”,在products/new.html.erb的表单中设置为true或falseRuby on rails 为什么要获取ActionView::Template::Error:未定义的方法`isebook';for nil:Heroku上的NilClass,但不是本地的,ruby-on-rails,heroku,Ruby On Rails,Heroku,我有一个Rails 4应用程序在本地运行SQLite,在Heroku上运行PostgreSQL 我有一个products类,它有一个布尔列“isebook”,在products/new.html.erb的表单中设置为true或false <%= form_for(@product, :html => {:multipart => true}) do |f| %> <% if @product.errors.any? %> <d
<%= 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">×</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 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
def新建
#仅当用户有剩余产品时才制作新产品
@product=product.new
如果当前_user.prodused<当前_user.prodlimit
用(@product)响应_
其他的
将\u重定向到用户\u路径(当前的\u user.id),错误:“您已达到产品限制。请升级您的帐户以添加更多产品。”
结束
结束
这是产品型号:
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
类产品URI::regexp(%w(http https))
属于:用户
已附加文件:image,:styles=>{:medium=>“300x300”,:thumb=>“200x200”},:default\u url=>“/images/:style/missing.jpg”
结束
这在本地正常工作,当创建新产品时,isebook字段根据复选框是否选中而指定为true或false
我现在尝试在Heroku上进行测试,我清除了整个生产数据库,并运行了所有迁移文件以清除所有现有产品。然后,我成功创建了一个帐户,在尝试呈现products/new.html.erb时,我得到了错误ActionView::Template::error:nil:NilClas的未定义方法“isebook”
我清除了整个生产数据库,并运行了所有迁移文件以清除所有现有产品。然后我成功地创建了一个帐户
有什么产品吗
ActionView::Template::Error: undefined method `isebook' for nil:NilClas
此错误表示您在
nil
上调用了方法isebook
,可能意味着您试图加载一个或所有产品,但在您的情况下它们不存在,因此在nil
(nothing)上调用该方法会引发错误 发布整个表单的代码,以及相应的控制器操作(编辑以添加整个表单代码)和相应的控制器操作。从堆栈跟踪中,您可以找出导致问题的行吗?代码看起来不错,我不熟悉堆栈跟踪,但是当我用设置isebook字段的复选框注释掉div元素时,错误就不再发生了
ActionView::Template::Error: undefined method `isebook' for nil:NilClas