Get my domain in Rails controller
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
)
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.