【问题标题】:Rails unable to save unchecked checkboxRails 无法保存未选中的复选框
【发布时间】:2019-09-05 19:29:19
【问题描述】:

我刚刚注意到,当用户尝试删除表单上的检查时,它实际上并没有维护状态。

我的表单如下所示(使用 Slim)

=f.tb_label :marketing_systems
=hidden_field_tag 'course_group[marketing_systems][]', nil
.col-sm-10
 .multi-select
  table
   tbody
    -MarketingSystem.ordered.each do |system|
     -input_name = "course_group_marketing_system_#{system.id}"
     tr
      td = check_box_tag 'course_group[marketing_system_ids][]',
                 system.id,
                 @course_group.marketing_systems.include?(system),
                 id: input_name

我想在隐藏标签字段上设置一个 nil ,它会在为空时推送它。然而事实并非如此。是不是我的 check_box_tag 设置有问题,没有传递为空?

我已尝试将参数放在括号中,并在 check_box_tag 上放置checked = false,但这没有用。如果我添加 value = 0 那么它会引发一个参数太多的问题。如果未选中,我只是想通过删除 system.id 来更新数据。

【问题讨论】:

    标签: ruby-on-rails checkbox


    【解决方案1】:

    只需改用collection_check_boxes

    = form_for(@course_group) do |f|
      .col-sm-10
       .multi-select
         table
           f.collection_check_boxes(:marketing_systems, :id, :name) do |b|
             tr
               td
                 = b.label
               td
                 = b.check_box
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多