Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get domain url in initializer file, rails

i was wondering if it was possible to get the domain url ie

localhost or myapp.heroku.com

from an initializer file?

i believe the

request.url

and the request methods only work in controllers. is there an equivalent to the javascript

document.domain

that i could do in an initializer file? or could i embed the .rb file with javascript? or other alternatives to the request object?

ive tried a number of things that don't work such as

Rails.root 

and am having trouble trying to figure it out.

thank you = )

like image 717
Sasha Avatar asked Oct 08 '22 18:10

Sasha


2 Answers

i just changed one of my apps to a staging environment. i couldn't 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 a staging.rb file inside my environments folder (i just copied and pasted everything from my production file) and rails was able to infer the method

Rails.env.staging?

in my initializer

like image 184
Sasha Avatar answered Oct 10 '22 08:10

Sasha


Something like:

require 'socket'

Socket.gethostname
like image 41
xdazz Avatar answered Oct 10 '22 07:10

xdazz