Ruby-on-rails 未选中时,如何使 check_box_tag 发布“false”或“0”参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10280162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:17:19  来源:igfitidea点击:

How can I make a check_box_tag to post a 'false' or '0' parameter when unchecked?

ruby-on-railsruby-on-rails-3checkboxparams

提问by Darme

With the following check_box_tag:

使用以下 check_box_tag:

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

I can update the boolean_attribute in only one direction: from false to true.

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

When is unchecked by default (because object.boolean_attribute is false) and I check it and then submit the form, a :boolean_attribute => 1 parameter is posted.

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

But, when I try to update from true to false no param is passed, so the boolean_attribute remains true.

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

In other words, when is checked by default (because object.boolean_attribute is true) and I uncheck it and then submit the form, a :boolean_attribute => 0 is notposted.

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

How can I make this check_box_tag to post a :boolean_attribute => 0 parameter when unchecked?

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

From the api I can't figure out if there is some option to pass to easily achieve it: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag

从 api 我不知道是否有一些选项可以轻松实现它:http: //api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag

Thank you.

谢谢你。

EDIT

编辑

For some reason I cannot fathom, in my actual code (with a nested many-to-many association) the hidden_field_tag is not working.

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

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

Now I have the opposite problem: I can uncheck the checkbox and the preparation is updated as aspected, but if I check the checkbox it messes up the params.

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

Here are the posted params for the unchecked box:

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

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

Now see what a mess when I check the checkbox, beginning from "cooked"=>" ", for some reason Rails is closing the preparation_attributes hash too early!

现在看看当我选中复选框时,从“cooked”=>“”开始是多么混乱,出于某种原因,Rails 过早地关闭了 prepare_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"}

EDIT #2:

编辑#2

I think I ran into a Rails bug related to deep nested resource forms and param passing: https://github.com/rails/rails/issues/5937

我想我遇到了与深层嵌套资源表单和参数传递相关的 Rails 错误:https: //github.com/rails/rails/issues/5937

For now I made it to work with a select_tag:

现在我让它与 select_tag 一起工作:

<%= select_tag 'order[preparations_attributes][][cooked]', options_for_select({yes: 1, no: 0}, preparation.cooked? ? 1 : 0) %> 

I think that switching to a select_tag in order to avoid the "hidden_field gotcha" is an acceptable workaround.

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

Anyway, thank you for the answers!

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

回答by jdoe

check_box(w/o _tag) helper adds hidden field to address your problem for you:

check_box(w/o _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:Dealing with nested resources (Product accepts_nested_attributes_for:line_items)

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

Checking 2 of 3 checkboxes gives me the paramsas this:

检查 3 个复选框中的 2 个给了我params这样的:

{..., "product"=>{"title"=>"RoR book", "line_items_attributes"=>{"0"=>{"approved"=>"0", "id"=>"47"}, "1"=>{"approved"=>"1", "id"=>"48"}, "2"=>{"approved"=>"1", "id"=>"51"}}}, "commit"=>"Update Product", "action"=>"action1", "controller"=>"test"}

{..., "product"=>{"title"=>"RoR book", "line_items_attributes"=>{"0"=>{"approved"=>"0", "id"=>"47" }, "1"=>​​{"approved"=>"1", "id"=>"48"}, "2"=>{"approved"=>"1", "id"=>"51" }}},“提交”=>“更新产品”,“动作”=>“动作1”,“控制器”=>“测试”}

paramsas YAML for readability:

params作为 YAML 以提高可读性:

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

See? No hidden fields but checked/unchecked states are clearly distinguished.

看?没有隐藏字段,但已明确区分选中/未选中状态。

Having this paramsallows me to use one line of code to update associated line_items:

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

@product.update_attributes params[:product]

I hope it helps.

我希望它有帮助。

回答by Moz Morris

You could use a hidden field above the checkbox:

您可以使用复选框上方的隐藏字段:

<%= hidden_field_tag 'object[boolean_attribute]', nil %>

This way, even if your checkbox isn't checked, you'll still get nilsubmitted. Would that work for you?

这样,即使未选中您的复选框,您仍会被nil提交。这对你有用吗?

回答by viks

If anyone have column type boolean and using check_box_tagthen look at this. It worked for me. <%= hidden_field_tag 'order[preparations_attributes][][cooked]', 'false' %> <%= check_box_tag 'order[preparations_attributes][][cooked]', true, preparation.cooked? %>

如果有人有列类型 boolean 并使用check_box_tag然后看看这个。它对我有用。 <%= hidden_field_tag 'order[preparations_attributes][][cooked]', 'false' %> <%= check_box_tag 'order[preparations_attributes][][cooked]', true, preparation.cooked? %>

回答by steve hawkes

in my rails app I needed to add single quotes around the true and false.

在我的 rails 应用程序中,我需要在 true 和 false 周围添加单引号。

Original code

原码

<%= f.check_box :admin, {}, true, false %>

Updated Code

更新代码

<%= f.check_box :admin, {}, 'true', 'false' %>

I hope that helps somebody else!

我希望能帮助别人!

回答by Gaston

For array like checkboxes, you could use a hash too:

对于像复选框这样的数组,您也可以使用哈希:

= hidden_field_tag "ad_ids[#{ad.token}]" , false
= check_box_tag "ad_ids[#{ad.token}]" , true, true