如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
当前回答
你可以设置一个变量URI.parse(current_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,但带有子目录。我使用:
root_url(:only_path => false)
弃用警告:不支持使用#request_uri。请改用fullpath。
如果你想要更具体,也就是说,你知道你需要的路径:
link_to current_path(@resource, :only_path => false), current_path(@resource)
如果您所说的相对是指没有域,那么请查看request.domain。