用下面的check_box_tag:
<%= check_box_tag 'object[boolean_attribute]', 1, object.boolean_attribute %>
我只能在一个方向更新boolean_attribute:从虚假到真实的。
当默认为未选中(因为object.boolean_attribute是假的),我检查,然后提交表单,答:boolean_attribute => 1个参数公布。
但是 ,当我尝试更新从真到假没有参数没有通过,所以boolean_attribute仍是如此。
换句话说,默认选中时(因为object.boolean_attribute是真实的),我取消它,然后提交表单,一个:boolean_attribute => 0 不贴。
未选中时boolean_attribute => 0参数:我怎样才能让这个check_box_tag张贴?
从API我想不通,如果有一些选项传递轻松实现它: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag
谢谢。
编辑
出于某种原因,我无法捉摸,在我的实际代码(带嵌套许多-to-many关联)的hidden_field_tag不工作。
<%= hidden_field_tag 'order[preparations_attributes][][cooked]', nil %>
<%= check_box_tag 'order[preparations_attributes][][cooked]', '1', preparation.cooked? %>
现在我有相反的问题:我可以取消选中该复选框,并为aspected准备进行更新,但如果我选中复选框它搅乱了PARAMS。
以下是未选中的方框张贴PARAMS:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bGgPGbk+Cuk2q+LEgqetmk4e7xie8dB3iMP9Cj3SUm0=", "order"=>{"customer_name"=>"Duccio Armenise", "duedate"=>"2012-04-25 09:24:00.000000", "preparations_attributes"=>[{"quantity"=>"1", "description"=>"custom recipe", "kind"=>"custom", "cooked"=>"", "recipe_id"=>"9", "id"=>"86", "quantities_attributes"=>[{"ingredient_id"=>"", "qty"=>"", "_destroy"=>"0"}, {"ingredient_id"=>"11", "qty"=>"5.0", "id"=>"193", "_destroy"=>"0"}], "_destroy"=>"0"}], "add_preparation"=>{"recipe_id"=>""}}, "continue"=>"Confirm", "id"=>"31"}
现在看到什么乱七八糟的,当我选中该复选框,从“熟” =>””开始的,由于某种原因Rails正在关闭preparation_attributes哈希太早!
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bGgPGbk+Cuk2q+LEgqetmk4e7xie8dB3iMP9Cj3SUm0=", "order"=>{"customer_name"=>"Duccio Armenise", "duedate"=>"2012-04-25 09:24:00.000000", "preparations_attributes"=>[{"quantity"=>"1", "description"=>"custom recipe", "kind"=>"custom", "cooked"=>""}, {"cooked"=>"1", "recipe_id"=>"9", "id"=>"86", "quantities_attributes"=>[{"ingredient_id"=>"", "qty"=>"", "_destroy"=>"0"}, {"ingredient_id"=>"11", "qty"=>"5.0", "id"=>"193", "_destroy"=>"0"}], "_destroy"=>"0"}], "add_preparation"=>{"recipe_id"=>""}}, "continue"=>"Confirm", "id"=>"31"}
编辑#2:
我觉得我碰到了与深嵌套的资源形式和param传递一个Rails错误: https://github.com/rails/rails/issues/5937
现在我做了一个select_tag工作:
<%= select_tag 'order[preparations_attributes][][cooked]', options_for_select({yes: 1, no: 0}, preparation.cooked? ? 1 : 0) %>
我认为,为了避免“hidden_field疑难杂症”切换到select_tag是可接受的解决方法。
无论如何,感谢您的答案!