(james pozdena) Blog Hash.to_url_params (nested)

Google couldn’t find me a good one. So I made this one:

  class Hash  
    def to_url_params  
      elements = []  
      self.each_pair do |key, value|  
        elements << param_for(key, value).flatten  
      end  
      elements.join('&')  
    end  
    private  
    def param_for(key, value, parent = nil)  
      if value.is_a?(Hash)  
        temp = []  
        value.each_pair do |key2, value2|  
          temp << param_for(key2, value2, parent ? parent + "[#{key}]" : key.to_s)  
        end  
        return temp  
      else  
        return ["#{parent ? parent + "[#{key}]" : key.to_s}=#{value}"]  
      end  
    end  
  end

Will transform:

  {:person =>{:name => 'Bobby', :age=>'28'}}

Into:

  person[name]=Bobby&person[age]=28
blog comments powered by Disqus
contact