Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

strange behavior with checkbox in nested form model

#1893

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...

Reported by Jon Pospischil · February 6th, 2009 @ 03:43 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Eloy Duran
    Eloy Duran

    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.

    February 6th, 2009 @ 10:47 AM

  2. Jon Pospischil
    Jon Pospischil

    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...

    February 6th, 2009 @ 03:30 PM

  3. Eloy Duran
    Eloy Duran

    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.

    February 6th, 2009 @ 03:39 PM

  4. Eloy Duran
    Eloy Duran

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

    February 6th, 2009 @ 03:39 PM

  5. Jon Pospischil
    Jon Pospischil

    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.

    February 6th, 2009 @ 03:45 PM

  6. Vidmantas
    Vidmantas
    • 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.

    August 31st, 2009 @ 08:39 AM

  7. Kevin
    Kevin

    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.

    November 21st, 2009 @ 01:38 AM

  8. Kevin
    Kevin

    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.

    November 21st, 2009 @ 01:44 AM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  10. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:41 PM

  11. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:41 PM