nonopolarity
nonopolarity

Reputation: 150996

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?

Upvotes: 1

Views: 506

Answers (3)

Aaron Hinni
Aaron Hinni

Reputation: 14716

This will get you what request.url gets you, but without the path:

request.protocol + request.host_with_port

Upvotes: 3

Dan
Dan

Reputation: 1509

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

Upvotes: 0

Spyros
Spyros

Reputation: 48626

You should be able to do it with request.domain.

Upvotes: 0

Related Questions