如何在Ruby on Rails视图中获得当前的绝对URL ?

请求。request_uri只返回相对URL。


当前回答

您可以在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

其他回答

 url_for(params)

你可以很容易地添加一些新参数:

url_for(params.merge(:tag => "lol"))
(url_for(:only_path => false) == "/" )? root_url : url_for(:only_path => false)

你可以使用:

request.full_path

or

request.url

希望它能解决你的问题。

干杯

你可以使用url_for(only_path: false)

request.env["REQUEST_URI"]

工作在rails 2.3.4测试,不知道其他版本。