2017-07-07 46 views
1

我正在使用散列來生成URL的參數。我有這樣的事情:帶重複鍵的Ruby散列,用於創建URL參數

params = { 
    :center => Geocoder.coordinates(currentlocation).join(","), 
    :zoom => 10, 
    :size => "460x280", 
    :markers => Geocoder.coordinates(markerlocation).join(","), 
    :sensor => true, 
    :key => ENV["GOOGLE_API_KEY"] 
    } 

query_string = params.map{|k,v| "#{k}=#{v}"}.join("&") 
image_tag "https://maps.googleapis.com/maps/api/staticmap?#{query_string}", :alt => location 

但是我需要在URL中有多個「標記」參數。對於我生成的每個網址,我都不知道需要多少個「標記」參數。例如,如果我有一個數組markerlocations,我需要爲該數組的每個成員創建一個:markers鍵值對,以便在URL中使用。什麼是完成這個最好的方法?

回答

1
require 'net/http' 
params = { 
    :center => Geocoder.coordinates(currentlocation).join(","), 
    :zoom => 10, 
    :size => "460x280", 
    :markers => [Geocoder.coordinates(markerlocation).join(",")], 
    :sensor => true, 
    :key => ENV["GOOGLE_API_KEY"] 
    } 
query_string = URI.encode_www_form(params) 
image_tag ...