ruby 给url添加参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7785793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:23:47  来源:igfitidea点击:

Add parameter to url

rubyurlparameters

提问by manuels

I have a url (e.g. http://www.youtube.com/watch?v=og9B3BEnBHo) and I'd like to add a parameter to it (wmode=opaque) so that its:

我有一个网址(例如http://www.youtube.com/watch?v=og9B3BEnBHo),我想向它添加一个参数(wmode=opaque),以便它:

http://www.youtube.com/watch?v=og9B3BEnBHo&wmode=opaque

http://www.youtube.com/watch?v=og9B3BEnBHo&wmode=opaque

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

谁能告诉我使用哪个函数来完成这项工作?

回答by steenslag

require 'uri'

uri =  URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo")
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("http://www.youtube.com/watch?v=og9B3BEnBHo")
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 Stringensures that this also works in the case when the original URI does not have a query string)

(调用String确保这在原始 URI 没有查询字符串的情况下也有效)

回答by Nick Q.

As Ruby has evolved over the years the answer differs between versions.

随着 Ruby 多年来的发展,不同版本的答案各不相同。

After 1.9.2

1.9.2之后

Ruby 1.9.2 saw decode_www_formand encode_www_formadded to the URI module, making encoding parameters easier.

Ruby 1.9.2 看到decode_www_formencode_www_form添加到 URI 模块,使编码参数更容易。

require 'uri'

uri = URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo")
new_query_ar = URI.decode_www_form(uri.query || '') << ["wmode", "opaque"]
uri.query = URI.encode_www_form(new_query_ar)
puts uri.to_s

Explanation

解释

URI.decode_www_formbreaks a string of query parameters (uri.query) into a nested array of parameters ([["v", "og9B3BEnBHo"]])

URI.decode_www_form将一串查询参数 ( uri.query) 分解成一个嵌套的参数数组 ( [["v", "og9B3BEnBHo"]])

uri.query || ''supplies either the query string of the uri, or if it does not exist, an empty string. This prevents decode_www_formfrom running into an error if uri.queryis nil.

uri.query || ''提供 uri 的查询字符串,或者如果它不存在,则提供一个空字符串。这可以防止decode_www_formuri.queryis 时遇到错误nil

<< ["wmode", "opaque"]adds another element to the array of query parameters. You may add more by further extending new_query_ar: new_query_ar << ["fullscreen", "1"]

<< ["wmode", "opaque"]将另一个元素添加到查询参数数组中。您可以通过进一步扩展来添加更多内容new_query_arnew_query_ar << ["fullscreen", "1"]

URI.encode_www_formencodes the nested array new query parameters into a string.

URI.encode_www_form将嵌套数组新查询参数编码为字符串。

Before 1.9.2

1.9.2 之前

require 'uri'

uri = URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo")
uri.query = [uri.query, "wmode=opaque"].compact.join('&') 
puts uri.to_s

Explanation

解释

[uri.query, "wmode=opaque"]is an array of all the eventual query parameters. You may add more by extending the array: [uri.query, "wmode=opaque", "fullscreen=1"]or by adding to the final element: "wmode=opaque&fullscreen=1"

[uri.query, "wmode=opaque"]是所有最终查询参数的数组。您可以通过扩展数组来添加更多内容:[uri.query, "wmode=opaque", "fullscreen=1"]或添加到最终元素:"wmode=opaque&fullscreen=1"

compactremoves nilelements from an array, thus it removes uri.queryif there is not an existing query parameter.

compactnil从数组中删除元素,因此uri.query如果没有现有的查询参数,它就会删除。

join, finally, joins them into a query string.

join,最后,将它们连接成一个查询字符串。

回答by Tyler Rick

Since you may have multiple query parameters you want to add and not just one, here's a version that lets you append as many params as you want by simply passing in a hash ... Plus a Hash seems like a more natural way to pass in paramsanyway, even if you're only adding one param.

由于您可能有多个查询参数要添加,而不仅仅是一个,这里有一个版本,它允许您通过简单地传递一个散列来附加任意数量的参数......加上一个散列似乎是一种更自然的传递方式params无论如何,即使您只添加一个参数。

  require 'uri'
  def add_params(url, params = {})
    uri = URI(url)
    params    = Hash[URI.decode_www_form(uri.query || '')].merge(params)
    uri.query =      URI.encode_www_form(params)
    uri.to_s
  end

Examples:

例子:

pry(main)> helper.add_params("http://example.com", b: 2)
=> "http://example.com?b=2"

pry(main)> helper.add_params("http://example.com?a=1", b: 2, c: 3)
=> "http://example.com?a=1&b=2&c=3"

pry(main)> helper.add_params("http://www.youtube.com/watch?v=og9B3BEnBHo", wmode: 'opaque')
=> "http://www.youtube.com/watch?v=og9B3BEnBHo&wmode=opaque"

回答by KNejad

Another option is to use the Addressable gem

另一种选择是使用Addressable gem

https://github.com/sporkmonger/addressable

https://github.com/sporkmonger/addressable

Once you include Addressable in your project all you will have to do is:

一旦你在你的项目中包含 Addressable,你所要做的就是:

url = Addressable::URI.parse('http://www.youtube.com/watch?v=og9B3BEnBHo')
url.query_values = url.query_values.merge(wmode:"opaque")

回答by Richard

In recent versions of the URI module, you can simply do:

在 URI 模块的最新版本中,您可以简单地执行以下操作:

original_uri = 'http://www.youtube.com/watch?v=og9B3BEnBHo'
append_params = { 'wmode': 'opaque' }

uri = URI.parse(original_uri)
params = URI.decode_www_form(uri.query || '') + append_params.to_a
uri.query = URI.encode_www_form(params)
puts uri.to_s