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? -- Posted via http://www.ruby-forum.com/.
Julian ''Julik'' Tarkhanov
2006-Jun-14 22:18 UTC
[Rails] Converting Hash to and from URL params
On 14-jun-2006, at 23:54, Alex Wayne 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 Wayne 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.nlThanks 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 -- Posted via http://www.ruby-forum.com/.
Alex Wayne 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 :-) -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser wrote in post #871670:> Alex Wayne 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgI 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.