如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
如何在Ruby on Rails视图中获得当前的绝对URL ?
请求。request_uri只返回相对URL。
当前回答
看起来在Ruby on Rails 3中request_uri已弃用。
Using #request_uri is deprecated. Use fullpath instead.
其他回答
看起来在Ruby on Rails 3中request_uri已弃用。
Using #request_uri is deprecated. Use fullpath instead.
如果你想要更具体,也就是说,你知道你需要的路径:
link_to current_path(@resource, :only_path => false), current_path(@resource)
Rails 4
控制器:
def absolute_url
request.base_url + request.original_fullpath
end
4.2版中显著的变化:
link_to和url_for在模板中默认生成绝对url,不再需要传递only_path: false。(提交)
观点:
如果使用_url后缀,则生成的URL是绝对的。使用路径获取相对URL。
<%= link_to "Home", root_url %>
欲知详情,请浏览:
http://blog.grepruby.com/2015/04/absolute-url-full-url-in-rails-4.html
对于Ruby on Rails 3:
request.url
request.host_with_port
我启动了一个调试器会话并查询了请求对象:
request.public_methods
获取不带任何查询参数的请求URL。
def current_url_without_parameters
request.base_url + request.path
end