How do I get the current absolute URL in Ruby on Rails?

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

The request.request_uri only returns the relative URL.


For Rails 3.2 or Rails 4+

You should use request.original_url to get the current 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

For Rails 3:

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


For Rails 2:

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 3.0方法现在是request.fullpath


你可以使用url_for(:only_path => false)

链接地址: http://www.djcxy.com/p/4312.html

上一篇: 执行shell命令时使用的键?

下一篇: 如何在Ruby on Rails中获取当前的绝对URL?