【问题标题】:How to update a record with unchecked checkboxes?如何使用未选中的复选框更新记录?
【发布时间】:2017-03-13 05:59:11
【问题描述】:

我在前端有三行复选框

当我检查一些值然后将参数传递给控制器​​时,它们看起来像数字。例如,

"service_attributes"=>{"id"=>1, "simplicity"=>"1", "convenience"=>"3", "atmosphere"=>"5"}

每次我更改任何行的值时,记录都会更新,但是当我取消选中选项时,不会传递参数

"service_attributes"=>{"id"=>1}

然后什么也没有发生。记录保留旧值。

我该如何做到这一点,当我取消选中复选框时,记录应该更新为 0 值。像下面这样:

<Service id: 1, simplicity: 0, convenience: 0, atmosphere: 2, user_id: 1>

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5


    【解决方案1】:

    您需要添加另一个将值 false 传递给控制器​​的字段。

    <input name="service_attributes[simplicity]" type="hidden" value="0" />
    <input checked="checked" type="checkbox" id="service_attributes_ simplicity" name="service_attributes[simplicity]" value="1" />
    

    或者您可以简单地使用checkbox_helper

    【讨论】:

    • 谢谢你,杰瑞米!
    • 很高兴能帮上忙!
    猜你喜欢
    • 2016-01-11
    • 2012-11-20
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多