我在 Rails 3.1 模型中有一些 bool 属性,而我刚刚通过迁移添加的两个新属性在 Heroku (Cedar) 上无法正常工作。它们在本地正常工作,我也在其中使用 PostgreSQL(第 9 版)。
迁移:
class AddNotificationSettingsToCollections < ActiveRecord::Migration
def change
add_column :collections, :email_comments, :boolean , :default => true
add_column :collections, :email_selections, :boolean , :default => true
end
end
查看(HAML)
%li
%label{:for => 'collection_email_comments'}
= f.check_box :email_comments
Email me when comments are made
%li
%label{:for => 'collection_email_selections'}
= f.check_box :email_selections
Email me when a selection is made
问题是,复选框总是显示为未选中,但当我检查控制台时,模型总是将属性设置为 true。当我跟踪 Heroku 日志文件时,我可以看到为这些字段 (1) 设置了正确的参数。
我错过了什么吗?我在这种形式中还有其他 bool 字段可以正常工作。这可能与默认值有关吗?
最佳答案
我也遇到了这个问题。我最终得到了一个愚蠢的解决方法。我知道这不是一个好的解决方案,但这是我的:
= f.check_box :email_comments, {:checked => (@collection.new_record? ? true : @collection.active)}
它很丑陋,但它在 Heroku 上以相同的设置为我完成了工作。希望有一个更优雅的解决方案......
关于ruby-on-rails - Heroku 上的 Rails check_box 行为不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8071283/