Ruby on Rails 如何获取当前 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21430151/
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
Ruby on Rails how to get current URL?
提问by Wagner Moreira
I'm trying to get the current url in rails using request.host, request.urlbut I have a website that are in 2 domains, so:
我正在尝试使用 获取 rails 中的当前 url request.host,request.url但我有一个位于 2 个域中的网站,因此:
when I'm at www.example1.comthe request.hostor .url show www.example1.com
当我在www.example1.com的request.host或的.url秀www.example1.com
when I'm at www.example2.comthe request.hostor .url show example1.combecause the example2 is getting all content from example1 and showing in the example2(parsed)
当我在www.example2.com的request.host或.URL显示example1.com,因为例题正从例1的所有内容,并在例2显示(解析)
because the request.host get really the host, and request.url getting the host also I'm really need the get the url in the browser don't the host, someone can help?
因为 request.host 真的是主机,而 request.url 也是主机,我真的需要在浏览器中获取 url 不是主机,有人可以帮忙吗?
OBs: I'm using rails 4 and ruby 2 OBs2: request.domain don't works, this return just ".com" of url
OB:我正在使用 rails 4 和 ruby 2 OBs2:request.domain 不起作用,这仅返回 url 的“.com”
回答by Alok Anand
request.original_url
shall provide you current url in rails 4.
应在 Rails 4 中为您提供当前 url。
You can visit this resource for more info @ How do I get the current absolute URL in Ruby on Rails?
您可以访问此资源以获取更多信息 @How do I get the current absolute URL in Ruby on Rails?

