How to get domain url in initialization file, rails

I was interested to know if I managed to get the url domain, i.e.

localhost or myapp.heroku.com 

from the initializer file?

I think that

 request.url 

and request methods work only in controllers. is there any javascript equivalent

 document.domain 

which could i do in the initialization file? or can i insert a .rb file with javascript? or other alternatives to the request object?

ive tried a few things that don't work, for example

 Rails.root 

and I'm having trouble trying to figure this out.

thanks =)

+4
source share
3 answers

I just changed one of my applications in an intermediate environment. I could not get the socket to work.

 heroku config:add RAILS_ENV=staging --app myapp-staging heroku config:add RACK_ENV=staging --app myapp-staging 

and then I made the staging.rb file in my environment folder (I just copied and embedded everything from my production file), and the rails were able to output the method

 Rails.env.staging? 

in my initializer

+1
source

Sort of:

 require 'socket' Socket.gethostname 
+1
source

Your application can determine the domain only when serving a request. (Thus, it is available for controllers in request .) In the initializer, which is executed once, at startup, before submitting any requests, this is not possible. (If you, the maintainer, do not know the domain in advance, and specify it in the configuration, for example, in an environment variable.)

Even one application can work in several domains. Then each request can have a different domain.

0
source

Source: https://habr.com/ru/post/1413465/


All Articles