简体   繁体   English

在rails中生成外部URL

[英]Generating an external url in rails

I want to construct a url within my rails app that points to another server that isn't running rails. 我想在我的rails应用程序中构建一个url,指向另一个没有运行rails的服务器。 Using url_for almost satisfies my requirements, but it requires a controller key which I don't need (redirecting to a top level page on the external site). 使用url_for几乎满足我的要求,但它需要一个我不需要的控制器密钥(重定向到外部站点的顶级页面)。

The reason I want to do this is so that I have a cleanly construct a url with a hash of arguments (some of which are determined at runtime). 我想这样做的原因是我有一个干净的构造一个带有参数散列的url(其中一些是在运行时确定的)。

You can call to_query on Hash in rails which will take care of url encoding etc. So maybe something like this: 你可以在rails中调用to_query来处理url编码等等。所以可能是这样的:

params = {
  :a => "http://google.com",
  :b => 123
}
url = "http://example.com?#{params.to_query}"

你怎么看待URI :: HTTP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM