Skip to content

Instantly share code, notes, and snippets.

@dejan
Created June 4, 2009 09:27
Show Gist options
  • Save dejan/123536 to your computer and use it in GitHub Desktop.
Save dejan/123536 to your computer and use it in GitHub Desktop.
Nesting params in Rails
>> params = {"line_items"=>[{"quantity"=>"1"}, {"quantity"=>"2"}]}
=> {"line_items"=>[{"quantity"=>"1"}, {"quantity"=>"2"}]}
>> query = CGI::unescape(params.to_query)
=> "line_items[][quantity]=1&line_items[][quantity]=2"
>> parsed_params = ActionController::AbstractRequest.parse_query_parameters(query)
=> {"line_items"=>[{"quantity"=>"1"}, {"quantity"=>"2"}]}
>> params == parsed_params
=> true
>> params = {"line_items"=>[{"quantity"=>"1", "sales_taxes"=>["11", "12"]}, {"quantity"=>"2", "sales_taxes"=>["22", "23"]}]}
=> {"line_items"=>[{"sales_taxes"=>["11", "12"], "quantity"=>"1"}, {"sales_taxes"=>["22", "23"], "quantity"=>"2"}]}
>> query = CGI::unescape(params.to_query)
=> "line_items[][quantity]=1&line_items[][sales_taxes][]=11&line_items[][sales_taxes][]=12&line_items[][quantity]=2&line_items[][sales_taxes][]=22&line_items[][sales_taxes][]=23"
>> parsed_params = ActionController::AbstractRequest.parse_query_parameters(query)
=> {"line_items"=>[{"quantity"=>"1", "sales_taxes"=>[]}, {"quantity"=>"2", "sales_taxes"=>["12"]}, {"sales_taxes"=>["22"]}, {"sales_taxes"=>["23"]}]}
>> params == parsed_params
=> false # check out how much different they are
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment