【问题标题】:Rails check_box on Heroku not behaving properlyHeroku上的Rails check_box行为不正确
【发布时间】:2011-11-09 20:35:09
【问题描述】:

我在 Rails 3.1 模型中有一些布尔属性,而我刚刚通过迁移添加的两个新属性在 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) 设置了正确的参数。

我错过了什么吗?我在这种形式中还有其他可以正常工作的布尔字段。这可能与默认值有关吗?

【问题讨论】:

  • 当您有一个新的 Collection 实例(即新创建但尚未在数据库中)或已从数据库加载的 Collection 时,未选中复选框?
  • 我的在几个小时后才自行修复。 ://
  • 肯定与架构中的默认值无关。您可能想发布您的控制器操作代码。
  • 我遇到了类似的问题。在我的情况下,该值没有保存到数据库中。它在本地工作,但在 Heroku 上没有。重新启动测功机解决了这个问题。为可能遇到它的其他人或我未来的自己添加此评论!

标签: ruby-on-rails ruby-on-rails-3 postgresql heroku


【解决方案1】:

我也遇到了这个问题。我最终得到了一个愚蠢的解决方法。我知道这不是一个好的解决方案,但这是我的:

= f.check_box :email_comments, {:checked => (@collection.new_record? ? true : @collection.active)}

这很难看,但它在 Heroku 上以相同的设置为我完成了这项工作。希望有一个更优雅的解决方案......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    相关资源
    最近更新 更多