This project is archived and is in readonly mode.

#1893 ✓stale
Jon Pospischil

strange behavior with checkbox in nested form model

Reported by Jon Pospischil | February 6th, 2009 @ 03:43 AM | in 3.x

Sorry if this is vague, I am not familiar enough with the internals to give a great explanation...

I have a form for a model (Item), and in this form I have an array of nested models (Item Options).

When I added a checkbox to these nested models, the params split them up unexpectedly, and I think it has something to do with the hidden field (http://dev.rubyonrails.org/ticke...

To define the fields I am using fields_for : <% fields_for "item[item_option_attributes][]", item_option do |option| %> <%= option.collection_select :option_id, @options, :id, :name, {:prompt => 'Select option'}, :index => nil %> <%= option.text_field :our_price, :size => 5, :index => nil %> <%= option.select :is_default, [["Yes", true], ["No", false]], {},:index => nil %> <%= option.hidden_field :should_destroy, :index => nil, :class => 'should_destroy' %> <%= option.hidden_field :id, :value => item_option.id, :index => nil %>

this sends the following params: "item_option_attributes"=>[{"our_price"=>"1.00", "is_default"=>"false", "should_destroy"=>"", "id"=>"32", "option_id"=>"1"}]

I have an item_option_attributes= method in my Item model. When I replace the above option.select with: option.checkbox :is_default, :index => nil

the params sent back to my controller are: "item_option_attributes"=>[{"our_price"=>"1.00", "is_default"=>"1", "option_id"=>"1"}, {"is_default"=>"0", "should_destroy"=>"", "id"=>"30"}]

I hope this is clear, if not I am happy to give it another shot...

Comments and changes to this ticket

  • Eloy Duran

    Eloy Duran February 6th, 2009 @ 10:47 AM

    Could you indicate which version of Rails you are using? Because I want to make sure we don't confuse the addition of NestedAttributes to 2.3RC1 with this issue.

  • Jon Pospischil

    Jon Pospischil February 6th, 2009 @ 03:30 PM

    Sorry about that, this occurred on rails 2.2.2.

    I wouldn't be terribly to find the same issues exist everywhere with checkboxes though...

  • Eloy Duran

    Eloy Duran February 6th, 2009 @ 03:39 PM

    Thanks, I just needed to know if I need to take responsibility for this ticket because of my additions to 2.3, which appears not to be the case.

    It could be that this ticket is a duplicate of #1863 btw, you might want to look into that.

  • Eloy Duran

    Eloy Duran February 6th, 2009 @ 03:39 PM

    Although I gotta note that your explanation is more elaborate :)

  • Jon Pospischil

    Jon Pospischil February 6th, 2009 @ 03:45 PM

    Thanks Eloy,

    That is a bit different than what I experienced. In my case BOTH values were being passed, but in separate arrays for some unknown reason. If I can find some time I'll give it a shot in 2.3 to see if the same happens there.

  • Vidmantas

    Vidmantas August 31st, 2009 @ 08:39 AM

    • Tag set to nested form, check_box

    Helloy,

    this bug still exists in 2.3.3 and is quite annoying. I don't get exactly who is responsible for parsing such a query - in HTML it seems everything ok (first hidden, then "visible" input field which should override hidden value when checked) but in comes doubled to Rails controller.

  • Kevin

    Kevin November 21st, 2009 @ 01:38 AM

    I had this same problem with rails 2.3.4. I was really confused. Anyone else? As soon as I changed my nested checkbox to a Yes/No select I got it to work. Before that I had what Sir Jon Pospischil found in his original post. See how is_default is in both the nested and non-nested hash? Weird.

    I also see this other similar issue:

    https://rails.lighthouseapp.com/projects/8994/tickets/1863-check_bo...

    All I know is select solved it for me...could have been my code was bad.

  • Kevin

    Kevin November 21st, 2009 @ 01:44 AM

    I should add this check_box problem was affecting a later form id which then caused problems upon validation in the controller. When I changed the check_box to select, the other later form problem disappeared. The id of the later form field changed from location[listing_attributes][11][location_id] to the expected location_listing_attributes_11_location_id_2. Somehow the check_box (as far as i can tell) caused problems later and rails couldn't figure out how to create that later form id correctly so it defaulted the "id" field to what you would normally find in the "name" field.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:41 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:41 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages