Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7825463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:59:37+00:00 2026-06-02T08:59:37+00:00

With the following check_box_tag: <%= check_box_tag ‘object[boolean_attribute]’, 1, object.boolean_attribute %> I can update the

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:59:40+00:00Added an answer on June 2, 2026 at 8:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using many checkboxes in a page as the following: <%= check_box_tag(:one, value
How can I preserve the states of checkbox using session? I tried the following
Following on from this answer , I am having a spot of bother with
I have the following checkbox tag: <%= check_box_tag pub_#{pub.id}, pub.id %> When I do
I am using following code to check whether a check box on my website
I use the following HTML helper to generate check box: <%= Html.CheckBox(DeluxeFeature)%> Now I
The following is a jquery code that would run when a check box is
The following code adds a checkbox and a label to the calling node. My
I have the following code: function toggleChecked(status) { $(.checkbox).each( function() { $(this).attr(checked,status); }) }
I need to have a checkbox which ajax-submits a form. The following code throws

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.