未选中时,如何使 check_box_tag 发布“false"或“0"参数? [英] How can I make a check_box_tag to post a 'false' or '0' parameter when unchecked?

查看:18
本文介绍了未选中时,如何使 check_box_tag 发布“false"或“0"参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下 check_box_tag:

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

我只能在一个方向更新 boolean_attribute:从假到真.

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

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

换句话说,when 被默认选中(因为 object.boolean_attribute 是真的),我取消选中它然后提交表单,一个 :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',preparations.cooked?%>

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

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

参数:{"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"=>""}}, "继续"=>"确认", "id"=>"31"}

现在看看当我从 "cooked"=>" " 开始勾选复选框时有多混乱,出于某种原因,Rails 过早地关闭了 prepare_attributes 哈希!

参数:{"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}, prepare.cooked? ? 1 : 0) %>

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

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

解决方案

check_box (w/o _tag) 帮助程序添加隐藏字段来为您解决问题:

<%= check_box 'object', 'boolean_attribute', {}, 'true', 'false' %># 结果:<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 做 |li|= li.check_box :批准= li.label :approved, li.object.id%br= f.提交

检查 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"}}},提交"=>更新产品",动作"=>动作1",控制器"=>测试"}

params 作为 YAML 以提高可读性:

<块引用>

产品:书名:RoR书line_items_attributes:'0':批准:'0'编号:'47''1':批准:'1'编号:'48''2':批准:'1'编号:'51'

看到了吗?没有隐藏的字段,但选中/未选中状态是明确区分的.

拥有这个 params 允许我使用一行代码来更新相关的 line_items:

@product.update_attributes params[:product]

希望能帮到你.

With the following 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.

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.

But, when I try to update from true to false no param is passed, so the boolean_attribute remains 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 not posted.

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

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

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 '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!

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:

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

For now I made it to work with a 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.

Anyway, thank you for the answers!

解决方案

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

<%= 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)

= 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 params as this:

{..., "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"}

params as YAML for readability:

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 params allows me to use one line of code to update associated line_items:

@product.update_attributes params[:product]

I hope it helps.

这篇关于未选中时,如何使 check_box_tag 发布“false"或“0"参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆