manuels manuels - 1 year ago 101
Ruby Question

Add parameter to url

I have a url (e.g. and I'd like to add a parameter to it (wmode=opaque) so that its:

Can anyone tell me which function to use to make this work?

Answer Source
require 'uri'

uri =  URI.parse("")
uri.query = [uri.query, "wmode=opaque"].compact.join('&') 
puts uri.to_s

#edit Since 1.9.2 there are methods added to the URI module

uri =  URI.parse("")
new_query_ar = URI.decode_www_form(String(uri.query)) << ["wmode", "opaque"]
uri.query = URI.encode_www_form(new_query_ar)
puts uri.to_s

(The call to String ensures that this also works in the case when the original URI does not have a query string)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download