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条回答
伤终究还是伤i
2楼-- · 2018-12-31 17:34

you can use any one for rails 3.2:

request.original_url
or
request.env["HTTP_REFERER"]
or
request.env['REQUEST_URI']

I think it will work every where

"#{request.protocol}#{request.host}:#{request.port}#{request.fullpath}"
查看更多
美炸的是我
3楼-- · 2018-12-31 17:35

It looks like request_uri is deprecated in Ruby on Rails 3.

Using #request_uri is deprecated. Use fullpath instead.
查看更多
人气声优
4楼-- · 2018-12-31 17:36

I needed the application URL but with the subdirectory. I used:

root_url(:only_path => false)
查看更多
裙下三千臣
5楼-- · 2018-12-31 17:36

I think request.domain would work, but what if you're in a sub directory like blah.blah.com? Something like this could work:

<%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>

Change the parameters based on your path structure.

Hope that helps!

查看更多
查无此人
6楼-- · 2018-12-31 17:36

You can use the ruby method:

:root_url

which will get the full path with base url:

localhost:3000/bla
查看更多
人气声优
7楼-- · 2018-12-31 17:38

In Ruby on Rails 3.1.0.rc4:

 request.fullpath
查看更多
登录 后发表回答