× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to convert hash to query params and vice versa in Rails

Anshul Mengi
Oct 12, 2014
<p>Very often we have a requirement of converting a hash to query parameters and vice versa.</p><p>This can be done in the following way:</p><pre><code class="language-ruby">{'first_param' =&gt; 1, 'second_param' =&gt; 2}.to_query =&gt; "first_param=1&amp;second_param=2"</code></pre><p>Similary to convert query params back to a hash, this can be done:</p><pre><code class="language-ruby">Rack::Utils.parse_nested_query("first_param=1&amp;second_param=2") =&gt; {"first_param"=&gt;"1", "second_param"=&gt;"2"}</code></pre><p>I hope this will be helpful</p>
comments powered by Disqus