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?
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
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.
#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
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
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?
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
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.
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.