Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get my domain in Rails controller

I would like to get my domain name: http://localhost:3000 in dev, or mydomain.com in production.

I don't want to use request.host, because my request may come from a different website. How should I do this?

Thank you.

like image 744
AdamNYC Avatar asked Mar 18 '13 18:03

AdamNYC


2 Answers

You'd use request.domain. Take a look at the ActionController Overview - Request & Response Objects section for more info.

For clarification, request.domain will return the hostname of the server used for the request, not the hostname of the visitor. Unless you are hosting your application using multiple domains you should get the value you are expecting (e.g. mydomain.com)

like image 124
Dan Reedy Avatar answered Sep 21 '22 03:09

Dan Reedy


I know this is an old question but it ranks highly in search. In case anyone stumbles across this looking to retrieve the domain with the protocol, you can use:

request.base_url
#=> http://localhost:3000 # dev
#=> https://example.com # prod

Hope that's handy for someone.

like image 21
SRack Avatar answered Sep 22 '22 03:09

SRack