Most of the times we look up to pass extra params in url.
This post will give an overview of possible query string formation but in rails way.
For this, we know the common syntax for query string is "/controller/action?name='A'&id=1", where multiple params are separated by '&' literal.
Here are the common rails ways:
-
to_param => Returns a String, which Action Pack uses for constructing an URL to this object.
i) When operated over hash, it will assignment string. For say,=> {a: 1}.to_param => "a=1"
ii) When operated over array, it will string with each element separated by '/' literal. For say,
=> ['being' ,'reptile', 'frog'].`to_param` => "being/reptile/frog"
-
to_query => Converts an object into a string suitable for use as a URL query string, using the given key as the param name. This internally uses to_param.
i) When operated over hash, it will return string but representing nested hash. For say,=> {'a' => 1}.`to_query`('letter') => letter['a'] = 1
ii) When operated over array, it will string with each element separated by '/' literal. For say,
=> ['being' ,'reptile', 'frog'].`to_query`('living') => "living[]=being&living[]=reptile&living[]=frog" Note: living will internally be formed as {living: ['being' ,'reptile', 'frog'] }
Above methods will be helpful in case if we want to send an params over string-url(i.e '/controller/action') rather than using rails url-helpers (i.e user_path(@user)).