如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
当前回答
你可以用
request.original_url
或
"#{request.protocol}#{request.host_with_port}"
获取当前URL。
其他回答
在Rails 3中你可以使用
request.original_url
http://apidock.com/rails/v3.2.8/ActionDispatch/Request/original_url
我认为请求。域名可以,但如果你在像blah.blah.com这样的子目录中呢?这样做是可行的:
<%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
根据路径结构更改参数。
希望有帮助!
(url_for(:only_path => false) == "/" )? root_url : url_for(:only_path => false)
你可以用
request.original_url
或
"#{request.protocol}#{request.host_with_port}"
获取当前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