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.

UrlEncodedPairParser creates wrong params hash with nested attributes in arrays

#69

In edge rails, sending in request attributes with the following:

&email[to_new_contacts][][contact_attributes][first_name]=first

&email[to_new_contacts][][contact_attributes][middle_name]=middle

&email[to_new_contacts][relationship]=friend

&email[to_new_contacts][][contact_attributes][first_name]=first2

&email[to_new_contacts][][contact_attributes][middle_name]=middle2

&email[to_new_contacts][relationship]=friend2

Creates a very unexpected params value. The attached test compares what the desired output is, versus what is actually output by UrlEncodedPairParser.

Shouldn't the params hash look similar to the following? It looks much different, as you'll see in the test.

:email => :to_new_contacts => [

{:relationship => '..', : contact_attributes => { :first_name => '..' } },

{:relationship => '..', : contact_attributes => { :first_name => '..' } }

]

Reported by Matthew Moore · April 30th, 2008 @ 07:25 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Carl Porth
    Carl Porth

    In the following arguments, you are treating email[to_new_contacts] like an array with []

    &email[to_new_contacts][][contact_attributes][first_name]=first

    &email[to_new_contacts][][contact_attributes][middle_name]=middle

    &email[to_new_contacts][][contact_attributes][first_name]=first2

    &email[to_new_contacts][][contact_attributes][middle_name]=middle2

    However, in these arguments, you are treating email[to_new_contacts] like a hash by indexing it with "relationship"

    &email[to_new_contacts][relationship]=friend

    &email[to_new_contacts][relationship]=friend2

    You probably just want to use a hash like this:

    &email[to_new_contacts][friend][contact_attributes][first_name]=first

    &email[to_new_contacts][friend][contact_attributes][middle_name]=middle

    &email[to_new_contacts][friend2][contact_attributes][first_name]=first2

    &email[to_new_contacts][friend2][contact_attributes][middle_name]=middle2

    &email[to_new_contacts][relationship]=friend

    &email[to_new_contacts][relationship]=friend2

    Which will create these params:

    {"email"=>
      {"to_new_contacts"=>
        {"friend2"=>
          {"contact_attributes"=>
            {"first_name"=>"first2", "middle_name"=>"middle2"}},
         "relationship"=>"friend",
         "friend"=>
          {"contact_attributes"=>
            {"first_name"=>"first", "middle_name"=>"middle"}}}}}
    

    May 28th, 2008 @ 03:56 AM

  2. Stephen Heuer
    Stephen Heuer
    • Tag set to actionpack, bug, closed, request, urlencodedpairparser

    We are having the same problem. Except it seems to only be a problem when we are submitting via Ajax and using Form#serialize

    When submitted via Form#serialize looking in firebug, it shows that the this is how the attributes are being sent:

    user[phone_numbers_attributes][][extension]
    user[phone_numbers_attributes][][extension]
    user[phone_numbers_attributes][][phone_number] = 1234567899
    user[phone_numbers_attributes][][phone_number] = 1234567890
    user[phone_numbers_attributes][][phone_type] = cell
    user[phone_numbers_attributes][][phone_type] = Home
    user[phone_numbers_attributes][][primary]
    user[phone_numbers_attributes][][primary]
    

    This is what it looks like in the params object:

    {"user"=>
      {"phone_numbers_attributes"=>
        [
          {"primary"=>""}, 
          {"primary"=>"", "phone_number"=>"1234567890"}, 
          {"extension"=>"", "phone_number"=>"1234567899"}, 
          {"phone_type"=>"Home", "extension"=>""}, 
          {"phone_type"=>"Cell"}
        ]
      }
    }
    

    When submitted via normal form posting we get this in the params object:

    {"user"=>
      {"phone_numbers_attributes"=>
        [
          {"phone_type"=>"Home", "primary"=>"", "extension"=>"", "phone_number"=>"1234567899"}, 
          {"phone_type"=>"Cell", "primary"=>"", "extension"=>"", "phone_number"=>"1234567890"}
        ]
      }
    }
    

    Also, Why does submitting via the Ajax and Form#serialize do empty fields show up as a blank string, but normal form submitting does not?

    July 12th, 2008 @ 09:57 PM

  3. Stephen Heuer
    Stephen Heuer
    • Tag changed from actionpack, bug, closed, request, urlencodedpairparser to actionpack, bug, request, urlencodedpairparser

    Here is a link to the problem in the old rails trac:

    http://dev.rubyonrails.org/ticket/8775

    July 12th, 2008 @ 10:03 PM

  4. Comron Sattari
    Comron Sattari
    • Tag changed from actionpack, bug, request, urlencodedpairparser to actionpack, bug, request, urlencodedpairparser

    This old ticket is related as well, http://dev.rubyonrails.org/ticke.... I'm still having this problem, I was hoping it would have been fixed in 2.1.

    Comron

    September 17th, 2008 @ 09:40 PM

  5. Ivan Vanderbyl
    Ivan Vanderbyl

    A patch which (mostly) fixes this problem is available here http://www.vector-seven.com/2008...

    I'm using it in production with minor tweaks to the form helpers and I have had very few issues.

    Also an idea to compact! the arrays which are returned as they often contain nils.

    September 18th, 2008 @ 12:11 AM

  6. Pratik
    Pratik
    • State changed from new to incomplete

    We need a patch or even a failing test case. Will reopen after that.

    Thanks.

    December 19th, 2008 @ 03:58 PM

  7. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from actionpack, bug, request, urlencodedpairparser to actionpack, bug, bugmash, request, urlencodedpairparser

    August 4th, 2009 @ 05:15 PM

  8. Rizwan Reza
    Rizwan Reza
    • State changed from incomplete to stale
    • Tag changed from actionpack, bug, bugmash, request, urlencodedpairparser to actionpack, bug, request, urlencodedpairparser

    January 20th, 2010 @ 10:52 AM

  9. Thomas Pedersen
    Thomas Pedersen
    • Importance changed from to

    It would be nice to get this fixed. It has been three years now...

    October 13th, 2010 @ 04:21 AM

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

    Thomas can you provide a patch with a test case or at least a test case only?.
    Thanks.

    October 13th, 2010 @ 02:11 PM

  11. Thomas Pedersen
    Thomas Pedersen

    I have attached a view template that demonstrates the problem. You have to set up a simple controller and an action in routes.rb for it work, but that takes two minutes.

    The form conctains the following fields:







    When you submit the form normally, Rails groups the values proper in params[] like this:

    Parameters: {"commit"=>"Subtmit", "rule"=>{"condition_list"=>[{"text"=>"text1", "value"=>"value1"}, {"text"=>"text2", "value"=>"value2"}, {"text"=>"text3", "value"=>"value3"}]}}

    But when you use button_to_remove to submit via Ajax, the values are no longer properly grouped:

    Parameters: {"commit"=>"Submit", "authenticity_token"=>"ZW+GHWqfHCpp4rDp67xYHn2a7iOJlbgLg1YYYVZJb9o=", "id"=>"1", "rule"=>{"condition_list"=>[{"text"=>"text1"}, {"text"=>"text2"}, {"text"=>"text3", "value"=>"value1"}, {"value"=>"value2"}, {"value"=>"value3"}]}}

    October 13th, 2010 @ 05:19 PM

  12. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:27 AM

  13. Santiago Pastorino
    Santiago Pastorino

    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:48 PM

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

    February 2nd, 2011 @ 04:48 PM