[英]Heroku ActionView::Template::Error (undefined method `name' for nil:NilClass)
[英]Why do I get ActionView::Template::Error: undefined method `isebook' for nil:NilClass on Heroku but not locally
我有一個在本地運行SQLite並在Heroku上運行PostgreSQL的Rails 4應用程序。
我有一個產品類,該類具有一個布爾列“ 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">×</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上進行測試,清除了整個生產數據庫,並運行了所有遷移文件以清除所有現有產品。 然后,當嘗試渲染產品/new.html.erb時,我成功創建了一個帳戶,但出現錯誤ActionView :: Template :: Error:nil:NilClas的未定義方法“ isebook”
我清除了整個生產數據庫,並運行了所有遷移文件以清除所有現有產品。 然后我成功創建了一個帳戶
有沒有產品?
ActionView::Template::Error: undefined method `isebook' for nil:NilClas
此錯誤表示您已在nil
上調用了isebook
方法,可能意味着您嘗試加載一個或所有產品,但在您不使用它們的情況下,因此在nil
上調用該方法(無)會引發錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.