How do I get the current absolute URL in Ruby on R

2018-12-31 17:11发布

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

The request.request_uri only returns the relative URL.

30条回答
柔情千种
2楼-- · 2018-12-31 17:26

If you're using Rails 3.2 or Rails 4 you should use request.original_url to get the current URL.


Documentation for the method is at http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url but if you're curious the implementation is:

def original_url
  base_url + original_fullpath
end
查看更多
萌妹纸的霸气范
3楼-- · 2018-12-31 17:26

For rails 3 :

request.fullpath

查看更多
爱死公子算了
4楼-- · 2018-12-31 17:27

None of the suggestions here in the thread helped me sadly, except the one where someone said he used the debugger to find what he looked for.

I've created some custom error pages instead of the standard 404 and 500, but request.url ended in /404 instead of the expected /non-existing-mumbo-jumbo.

What I needed to use was

request.original_url
查看更多
爱死公子算了
5楼-- · 2018-12-31 17:28

For Ruby on Rails 3:

request.url
request.host_with_port

I fired up a debugger session and queried the request object:

request.public_methods
查看更多
回忆,回不去的记忆
6楼-- · 2018-12-31 17:34
临风纵饮
7楼-- · 2018-12-31 17:34

Rails 4.0

you can use request.original_url, output will be as given below example

get "/articles?page=2"

request.original_url # => "http://www.example.com/articles?page=2"
查看更多
登录 后发表回答