如何在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"
其他回答
Rails 4.0
你可以使用request。Original_url,输出将如下示例所示
get "/articles?page=2"
request.original_url # => "http://www.example.com/articles?page=2"
看起来在Ruby on Rails 3中request_uri已弃用。
Using #request_uri is deprecated. Use fullpath instead.
我认为请求。域名可以,但如果你在像blah.blah.com这样的子目录中呢?这样做是可行的:
<%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
根据路径结构更改参数。
希望有帮助!
你可以在rails 3.2中使用任何一个:
request.original_url
or
request.env["HTTP_REFERER"]
or
request.env['REQUEST_URI']
我认为它在任何地方都适用
"#{request.protocol}#{request.host}:#{request.port}#{request.fullpath}"
你可以设置一个变量URI.parse(current_url),我还没有看到这个建议在这里,它为我工作。