gpt4 book ai didi

ruby-on-rails - 如何使 check_box_tag 在未选中时发布 'false' 或 '0' 参数?

转载 作者:行者123 更新时间:2023-12-03 07:45:39 25 4
gpt4 key购买 nike

使用以下 check_box_tag:

<%= check_box_tag 'object[boolean_attribute]', 1, object.boolean_attribute %>

我只能在一个方向上更新 boolean_attribute:从 false 到 true。

当默认情况下未选中(因为 object.boolean_attribute 为 false)并且我选中它然后提交表单时,会发布 :boolean_attribute => 1 参数。

但是,当我尝试从 true 更新为 false 时,没有传递任何参数,因此 boolean_attribute 仍然为 true。

换句话说,当默认选中(因为 object.boolean_attribute 为 true)并且我取消选中它然后提交表单时,a :boolean_attribute => 0 不会发布。

如何使此 check_box_tag 在未选中时发布 :boolean_attribute => 0 参数?

从 API 中我无法弄清楚是否有一些选项可以传递来轻松实现它: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag

谢谢。

编辑

出于某种原因,我无法理解,在我的实际代码(具有嵌套的多对多关联)中,hidden_​​field_tag 不起作用。

<%= hidden_field_tag 'order[preparations_attributes][][cooked]', nil %>
<%= check_box_tag 'order[preparations_attributes][][cooked]', '1', preparation.cooked? %>

现在我遇到了相反的问题:我可以取消选中该复选框,并且准备工作会按方面更新,但如果我选中该复选框,则会弄乱参数。

以下是未选中框的已发布参数:

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"}

现在看看当我选中复选框时,从“cooked”=>“”开始,情况有多困惑,由于某种原因,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:

我想我遇到了与深层嵌套资源形式和参数传递相关的 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) %> 

我认为切换到 select_tag 以避免“hidden_​​field 陷阱”是一个可以接受的解决方法。

无论如何,谢谢您的回答!

最佳答案

check_box(无_tag)助手添加隐藏字段来为您解决问题:

<%= check_box 'object', 'boolean_attribute', {}, 'true', 'false' %>

# result:
<input name="object[boolean_attribute]" type="hidden" value="false" />
<input id="object_boolean_attribute" name="object[boolean_attribute]" type="checkbox" value="true" />

UPD:处理嵌套资源(产品accepts_nested_attributes_for :line_items)

= form_for @product, url: '' do |f|
%p
= f.label :title
= f.text_field :title

= f.fields_for :line_items do |li|
= li.check_box :approved
= li.label :approved, li.object.id
%br
= f.submit

检查 3 个复选框中的 2 个会给出 params,如下所示:

{..., "product"=>{"title"=>"RoR 书", "line_items_attributes"=>{"0"=>{"approved"=>"0", "id"=> "47"}, "1"=>​​{"已批准"=>"1", "id"=>"48"}, "2"=>{"已批准"=>"1", "id"=> “51”}}},“提交”=>“更新产品”,“操作”=>“操作1”,“ Controller ”=>“测试”}

params 作为 YAML 以提高可读性:

product:
title: RoR book
line_items_attributes:
'0':
approved: '0'
id: '47'
'1':
approved: '1'
id: '48'
'2':
approved: '1'
id: '51'

看到了吗?没有隐藏字段,但选中/未选中状态明显区分。

有了这个params,我就可以使用一行代码来更新关联的line_items:

@product.update_attributes params[:product]

希望对您有所帮助。

关于ruby-on-rails - 如何使 check_box_tag 在未选中时发布 'false' 或 '0' 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10280162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com