Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Converting URL strings to params?

I know Rails does this for you, but I have a need to do this myself for examples. Is there a simple, non-private method available that takes a string and returns the hash of params exactly as Rails does for controllers?

like image 608
user3281384 Avatar asked Dec 01 '22 18:12

user3281384


2 Answers

Using Rack::Utils.parse_nested_query(some_string) will give you better results since CGI will convert all the values to arrays.

like image 157
Vitali Margolin Avatar answered Dec 05 '22 01:12

Vitali Margolin


I found a way after a more intense Google search.

To convert url string to params:

hash = CGI::parse(some_string)

And (as bonus) from hash back to url string:

some_string = hash.to_query

Thanks to: https://www.ruby-forum.com/topic/69428

like image 34
user3281384 Avatar answered Dec 05 '22 02:12

user3281384