如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
当前回答
Rails 4.0
你可以使用request。Original_url,输出将如下示例所示
get "/articles?page=2"
request.original_url # => "http://www.example.com/articles?page=2"
其他回答
您可以在ApplicationController中添加这个current_url方法来返回当前URL,并允许合并其他参数
# https://x.com/y/1?page=1
# + current_url( :page => 3 )
# = https://x.com/y/1?page=3
def current_url(overwrite={})
url_for :only_path => false, :params => params.merge(overwrite)
end
使用示例:
current_url --> http://...
current_url(:page=>4) --> http://...&page=4
对于Rails 3。X向上:
#{request.protocol}#{request.host_with_port}#{request.fullpath}
对于Rails 3.2及更高版本:
request.original_url
因为在rails 3.2及更高版本中:
request.original_url = request.base_url + request.original_fullpath
欲了解更多信息,请访问http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url
对于Ruby on Rails 3:
request.url
request.host_with_port
我启动了一个调试器会话并查询了请求对象:
request.public_methods
你可以通过调用:
request.original_url
or
request.env['HTTP_REFERER']
看起来在Ruby on Rails 3中request_uri已弃用。
Using #request_uri is deprecated. Use fullpath instead.