带有以下复选框标记:

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

我只能在一个方向上更新boolean_属性:从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.

But,当我try 从true更新为false时,没有传递任何参数,因此布尔值_属性仍然为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?

从api中,我不知道是否有一些选项可以轻松实现:

非常感谢.

EDIT

由于某些原因,我无法理解,在我的实际代码(带有嵌套的多对多关联)中,隐藏的_字段_标记不起作用.

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

现在我遇到了相反的问题:我可以取消选中复选框,准备工作将更新为aspected,但如果我选中复选框,它会弄乱参数.

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

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:

我想我遇到了一个Rails bug,它与深度嵌套的资源表单和参数传递有关:https://github.com/rails/rails/issues/5937

目前,我使用了一个select_标签:

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

我认为切换到select_标签以避免"hidden_field gotcha"是一种可以接受的解决方法.

不管怎样,谢谢你的回答!

推荐答案

check_box(w/o _tag)helper添加隐藏字段以解决您的问题:

<%= 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:行项目)

= 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 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作为YAML,以确保可读性:

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

看见没有隐藏字段,但选中/未选中的状态可以清楚地区分.

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

@product.update_attributes params[:product]

我希望有帮助.

Ruby-on-rails相关问答推荐

ruby on rails:错误未知关键字::当使用google-cloud-storage下载文件时,soft_deleted

在VP中部署rails 7和nginx

Rails 7.1中的覆盖脚手架控制器

包含字母和数字组合的正则表达式

Rails 使用多个外键创建/构建

您如何查看在 Rails 中触发回调的关联模型?

从 rails 5 迁移到 rails 6

使用 RVM 时如何安装 Ruby gem?

Rails 将 form_for 对象传递给部分

如何避免 has_many :through 关系中的重复?

使用 ActiveRecord 3 / Arel 查找单个记录的最佳方法?

Rails data-disable-with re-enable 按钮

Rails:如何在表单的必填字段上禁用星号?

我如何找出为什么我不能#destroy() 记录?

Ruby - 合并两个数组并删除重复的值

如何在同一个 Rails 控制器操作中处理多个 HTTP 方法

设计:在注册期间禁用密码确认

使用 ruby​​ on rails 活动记录插入多条记录

我的 JavaScript 模式/实践很糟糕.我应该go 哪里寻求帮助?

如何在 rake 任务中强制 RAILS_ENV?