如何在 Ruby on Rails 中获取当前的绝对 URL? [英] How do I get the current absolute URL in Ruby on Rails?

查看:40
本文介绍了如何在 Ruby on Rails 中获取当前的绝对 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get the current absolute URL in my Ruby on Rails view?

request.request_uri 只返回相对的 URL.

推荐答案

对于 Rails 3.2 或 Rails 4+

您应该使用 request.original_url 来获取当前 URL.在此处找到当前存储库的源代码.

For Rails 3.2 or Rails 4+

You should use request.original_url to get the current URL. Source code on current repo found here.

此方法记录在 original_url 方法,但如果你好奇,实现是:

This method is documented at original_url method, but if you're curious, the implementation is:

def original_url
  base_url + original_fullpath
end


对于 Rails 3:

您可以编写 "#{request.protocol}#{request.host_with_port}#{request.fullpath}",因为 request.url 现在已被弃用.


For Rails 3:

You can write "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.

您可以编写 request.url 而不是 request.request_uri.这将协议(通常是 http://)与主机和 request_uri 结合起来为您提供完整地址.

You can write request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address.

这篇关于如何在 Ruby on Rails 中获取当前的绝对 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆