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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With