Converting Hash to and from URL params

I’m sure this is easy since rails does this internally, but I need to
convert a string of url formatted variables
“foo=bar&baz=snoo&why=because” to a hash, and I need to convert a hash
to url format.

Now before I roll my own methods to do this, does rails expose this
functionality somehow?

On 14-jun-2006, at 23:54, Alex W. wrote:

I’m sure this is easy since rails does this internally, but I need to
convert a string of url formatted variables
“foo=bar&baz=snoo&why=because” to a hash, and I need to convert a hash
to url format.

Now before I roll my own methods to do this, does rails expose this
functionality somehow?

Not publicly. Look for build_query_string in the Actionpack sources,
in routing.rb

I have been pathing on it recently but I forgot where it is exactly.


Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl

Julian ‘Julik’ Tarkhanov wrote:

On 14-jun-2006, at 23:54, Alex W. wrote:

I’m sure this is easy since rails does this internally, but I need to
convert a string of url formatted variables
“foo=bar&baz=snoo&why=because” to a hash, and I need to convert a hash
to url format.

Now before I roll my own methods to do this, does rails expose this
functionality somehow?

Not publicly. Look for build_query_string in the Actionpack sources,
in routing.rb

I have been pathing on it recently but I forgot where it is exactly.


Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl

Thanks for the location info, but it was only partially useful. It gave
me some help writing my own methods, but since they are private in the
controller code they are innacessible.

For future searchers of similar problems, here is the solution I came up
with, implemented as an extension fo the Hash class.

allows:
{:foo => ‘bar’, :baz => ‘snoopy’}.to_url_params

returns “foo=bar&baz=snoopy”

Hash.from_url_params(“foo=bar&baz=snoopy”)

returns {:foo => ‘bar’, :baz => ‘snoopy’}

#code
class Hash
def to_url_params
elements = []
keys.size.times do |i|
elements << “#{keys[i]}=#{values[i]}”
end
elements.join(’&’)
end

def self.from_url_params(url_params)
result = {}.with_indifferent_access
url_params.split(’&’).each do |element|
element = element.split(’=’)
result[element[0]] = element[1]
end
result
end
end

Alex W. wrote:

Thanks for the location info, but it was only partially useful. It gave
me some help writing my own methods, but since they are private in the
controller code they are innacessible.

Might be private, but I would just call them with send :slight_smile:

Alex W. wrote:

I’m sure this is easy since rails does this internally, but I need to
convert a string of url formatted variables
“foo=bar&baz=snoo&why=because” to a hash, and I need to convert a hash
to url format.

Now before I roll my own methods to do this, does rails expose this
functionality somehow?

here’s the built-in way to do it.

s = hash.to_query

hash = CGI::parse(s)

You need to encode or you will get spaces in the strings…
class Hash
def to_url_params
elements = []
keys.size.times do |i|
elements << “#{CGI::escape(keys[i])}=#{CGI::escape(values[i])}”
end
elements.join(’&’)
end

def self.from_url_params(url_params)
result = {}.with_indifferent_access
url_params.split(’&’).each do |element|
element = element.split(’=’)
result[element[0]] = element[1]
end
result
end
end

Alex W. wrote:

I’m sure this is easy since rails does this internally, but I need to
convert a string of url formatted variables
“foo=bar&baz=snoo&why=because” to a hash, and I need to convert a hash
to url format.

Now before I roll my own methods to do this, does rails expose this
functionality somehow?

Hold it. Why are you doing this in the first place? If it’s part of
request processing or URL generation, Rails will already do it for you.
I’m not sure why you’d need this directly; please explain.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote in post #871670:

Alex W. wrote:

I’m sure this is easy since rails does this internally, but I need to
convert a string of url formatted variables
“foo=bar&baz=snoo&why=because” to a hash, and I need to convert a hash
to url format.

Now before I roll my own methods to do this, does rails expose this
functionality somehow?

Hold it. Why are you doing this in the first place? If it’s part of
request processing or URL generation, Rails will already do it for you.
I’m not sure why you’d need this directly; please explain.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I want to link to ‘/auth/facebook’ with parameters but that path is
handled by the omniauth gem and doesn’t appear in my routes file.

edit: Nevermind apparently there is a method called to_query now.