Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 是否有基于一组参数生成URL的Ruby库/gem?_Ruby On Rails_Ruby_Gem - Fatal编程技术网

Ruby on rails 是否有基于一组参数生成URL的Ruby库/gem?

Ruby on rails 是否有基于一组参数生成URL的Ruby库/gem?,ruby-on-rails,ruby,gem,Ruby On Rails,Ruby,Gem,Rails的URL生成机制(其中大多数在某一点通过polymorphic_URL路由)允许传递一个哈希,该哈希被序列化为一个查询字符串,至少用于GET请求。除了基本路径之外,获得这种功能的最佳方法是什么 例如,我希望有如下内容: generate_url('http://www.google.com/', :q => 'hello world') # => 'http://www.google.com/?q=hello+world' 我当然可以编写完全符合我的应用程序要求的自己

Rails的URL生成机制(其中大多数在某一点通过
polymorphic_URL
路由)允许传递一个哈希,该哈希被序列化为一个查询字符串,至少用于GET请求。除了基本路径之外,获得这种功能的最佳方法是什么

例如,我希望有如下内容:

generate_url('http://www.google.com/', :q => 'hello world')
  # => 'http://www.google.com/?q=hello+world'

我当然可以编写完全符合我的应用程序要求的自己的程序,但如果存在一些规范库来处理它,我宁愿使用它:)。

是的,在Ruby的标准库中,您可以找到一整套用于处理URI的类。有一个用于HTTP。您可以使用一些参数调用
#build
,就像您展示的那样

对于查询字符串本身,只需使用Rails的Hash addition
#进行查询
。i、 e

uri = URI::HTTP.build(:host => "www.google.com", :query => { :q => "test" }.to_query)

晚会迟到了,但让我强烈推荐宝石。除了其他有用的特性外,它还支持编写和解析uri的via。要调整给定的示例,请尝试:

gsearch=Addressable::Template.new('http://google.com/{?查询*}')
展开(查询:{:q=>'helloworld'})
# => "http://www.google.com/?q=hello%20world"

我建议使用我的gem,它可以通过流畅的界面轻松构建URL:

require 'iri'
url = Iri.new('http://google.com/')
  .append('find').append('me') # -> http://google.com/find/me
  .add(q: 'books about OOP', limit: 50) # -> ?q=books+about+OOP&limit=50
  .del(:q) # remove this query parameter
  .del('limit') # remove this one too
  .over(q: 'books about tennis', limit: 10) # replace these params
  .scheme('https') # replace 'http' with 'https'
  .host('localhost') # replace the host name
  .port('443') # replace the port
  .path('/new/path') # replace the path of the URI, leaving the query untouched
  .cut('/q') # replace everything after the host and port
  .to_s # convert it to a string
对于香草红宝石,请使用:


精彩的!我不能说我以前见过URI模块,所以这太酷了。
undefined method'to#query'for#(NoMethodError)
这只在使用rails时有效。如果不使用rails,还有其他选择吗?@StefanHendriks这不是真的:URI模块是一个标准的ruby库,它不需要rails。在使用此答案中的代码之前,必须发出
require'uri'
。否则它会像广告一样工作。
Hash\to\u query
的替代方法:
URI.encode\u www\u form
请检查此项,这将有助于提高您的内容质量将其清理并添加示例。谢谢@willie!我注意到Thor gem只对Ruby 1.8使用可寻址。在那之后有没有添加一些东西,这样就不再需要了?
require 'iri'
url = Iri.new('http://google.com/')
  .append('find').append('me') # -> http://google.com/find/me
  .add(q: 'books about OOP', limit: 50) # -> ?q=books+about+OOP&limit=50
  .del(:q) # remove this query parameter
  .del('limit') # remove this one too
  .over(q: 'books about tennis', limit: 10) # replace these params
  .scheme('https') # replace 'http' with 'https'
  .host('localhost') # replace the host name
  .port('443') # replace the port
  .path('/new/path') # replace the path of the URI, leaving the query untouched
  .cut('/q') # replace everything after the host and port
  .to_s # convert it to a string
require 'uri'
query = URI.encode_www_form({ :q => "test" })
url = URI::HTTP.build(:host => "www.google.com", query: query).to_s
#=> "http://www.google.com?q=test"