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:21

If by relative, you mean just without the domain, then look into request.domain.

查看更多
与君花间醉酒
3楼-- · 2018-12-31 17:21
request.env["REQUEST_URI"]

works in rails 2.3.4 tested and do not know about other versions.

查看更多
零度萤火
4楼-- · 2018-12-31 17:23

if you want to be specific, meaning, you know the path you need:

link_to current_path(@resource, :only_path => false), current_path(@resource)
查看更多
无与为乐者.
5楼-- · 2018-12-31 17:23

For Rails 3.2 or Rails 4 Simply get in this way "request.original_url" Reference: Original URL Method

For Rails 3 As request.url is deprecated.We can get absolute path by concatenating

"#{request.protocol}#{request.host_with_port}#{request.fullpath}"

For Rails 2

request.url
查看更多
孤独寂梦人
6楼-- · 2018-12-31 17:24

You can use:

request.full_path

or

request.url

Hopefully it will resolve your problem.

Cheers

查看更多
看风景的人
7楼-- · 2018-12-31 17:25

DEPRECATION WARNING: Using #request_uri is deprecated. Use fullpath instead.

查看更多
登录 后发表回答