Reputation: 6317
If I convert a hash to a query string, how can I convert it back again?
{:filters => {:colour => ['Red', 'Blue'], :size => 'Medium'}}.to_param
=> "filters[colour][]=Red&filters[colour][]=Blue&filters[size]=Medium"
Rails appears to do this automatically when it populates the params
hash, but is it possible to call this method directly?
Thanks.
Upvotes: 15
Views: 6030
Reputation: 107728
You're looking for Rack::Utils.parse_nested_query(query)
, which will convert it back into a Hash
. You can get it by using this line:
require 'rack/utils'
Upvotes: 31
Reputation: 9014
query_string = "filters[colour][]=Red&filters[colour][]=Blue&filters[size]=Medium"
CGI::parse(query_string)
Upvotes: 1