简体   繁体   中英

In Ruby on Rails, is there a clean way to get the URL but just the front part? (only up to the domain name)

In Rails (version 3), request.url can show

http://www.foo.com/products/123

but what about just to get

http://www.foo.com    

or

http://www.foo.com/

? There are 2 apparent ways, one is using regular expression, which is not very clean, and the other is

"#{request.scheme}://#{request.host}"

and it is kind of ugly either. Is there a cleaner to do it?

这将为您提供request.url可以为您带来的东西,但是没有路径:

request.protocol + request.host_with_port

您应该能够使用request.domain做到这一点。

Should be stored in the request object. request.domain should get the info you need but use this command to retrieve all the information from the object and have a look at what is stored there

raise request.inspect

more info can be found at

http://rails.nuvvo.com/lesson/6377-action-controller-the-request-and-response-objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM