【发布时间】: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">×</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