【问题标题】:How to default collection_check_boxes to checked?如何默认 collection_check_boxes 被选中?
【发布时间】:2017-03-30 06:24:15
【问题描述】:

我有这条线,我试图默认选中 <%= f.collection_check_boxes :committed, checked, Date::ABBR_DAYNAMES, :downcase, :to_s, %>

db t.text "committed".

我尝试了checkedtrue 的变体,但也许我忽略了一些东西。

这是它的Gist

【问题讨论】:

  • 当你在 db t.text "committed" 中说 - 你在说哪个表?您能简要介绍一下您正在研究的模型吗?
  • 同意。该属性必须与模型相关联。通常,当您正在寻找预先检查的项目时,我发现集合复选框最好来自另一个表。因为在你的控制器中你可以做嵌套属性
  • @AmitA 我用要点更新了这个问题。希望这能提供一个更好的主意,因为我不太确定您的要求。
  • @HunterStevens 感谢您提供详细信息!我用要点更新了这个问题。
  • 这是一种新习惯的形式,更新一个,或两者兼而有之?

标签: ruby-on-rails ruby checked


【解决方案1】:

这里是关于如何将选中作为默认值添加到 collection_check_boxes 表单助手的快速答案,因为我花了一些时间才弄清楚。把它分成一个块,你可以设置检查和添加类。更多信息请访问http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_check_boxes

<%= f.collection_check_boxes(:author_ids, Author.all, :id, :name) do |b| %>
  <%= b.label(class: "check_box") { b.check_box(checked: true, class: "add_margin") + b.text } %>
<% end %>

【讨论】:

    【解决方案2】:

    您正在使用form_for,因此f 是一个表单生成器。这意味着它绑定到您初始化它的对象,我们称之为@habit。由于您在表单构建器上调用collection_check_boxes,它会执行类似@habit.send(:commit) 的操作来咨询是否应该选中复选框,而目前(显然)不是。换句话说,如果你想使用 form_for ,你需要在模型本身中表示这个“一切都被检查”的事实。

    现在我不确定您的模型层是什么样的,所以我将介绍一些场景。如果您有这样的has_and_belongs_to_many 关系:

    class Habit < ActiveRecord::Base
      has_and_belongs_to_many :committed_days
    end
    
    class CommittedDay < ActiveRecord::Base
      has_and_belongs_to_many :habits
      # let's assume it has the columns :id and :name
      # also, let's assume the n:m table committed_days_habits exists
    end
    

    那么我认为最简单的方法是在控制器本身做这样的事情:

    def new
      @habit = Habit.new
      @habit.committed_day_ids = CommittedDay.all.map(&:id)
    end
    

    在你的 ERB 中做:

    <%= f.collection_check_boxes(:committed_day_ids, CommittedDay.all, :id, :name)
    

    现在,使用 has-and-belongs-to-many 执行此操作可能有点过头了,尤其是在一周中的几天(这意味着 CommittedDay 表有 7 条记录,每天一条,这有点尴尬) .所以你也可以考虑简单地将一个星期数组序列化到数据库中,然后确保该列的默认值包含所有这些。

    ERB 将与您写的类似:

    <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
    

    如果您使用 Postgres,您的课程可以很简单:

    class Habit < ActiveRecord::Base
    end
    

    序列化代码将在迁移中:

    # downcase is used since in the ERB you are using :downcase for the id method
    t.text :committed, default: Date::ABBR_DAYNAMES.map(&:downcase), array: true
    

    如果您不使用 Postgres,则可以使用与 DB 无关的 Rails 序列化:

    class Habit < ActiveRecord::Base
      serialize :committed, Array
    end
    

    然后您的迁移将如下所示:

    t.text :committed, default: Date::ABBR_DAYNAMES.map(&:downcase).to_yaml
    

    【讨论】:

    • 运行 rails g migration name_of_migration 并编辑文件。
    • 太棒了!你是一个活生生的救星。
    猜你喜欢
    • 2014-09-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多