Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set configuration constants depending on environment in rails

I would like to define a constant (like the admin-email-adress) depending on the environment. What is the easiest way to do this?

I'd like something like that, in development.rb (or test or production.rb):

ADMIN_EMAIL = "[email protected]"

And be able to access it by calling something like

ADMIN_EMAIL

Is there an easy way or do I have to do something like creating a module and initialize it and stuff (and in case you're wondering if I have any idea about this, unfortunately: I don't)

It works this way, but one has to restart the server, for the constants to take effect.

like image 989
Roland Studer Avatar asked May 19 '09 12:05

Roland Studer


People also ask

How do I see environment variables in Rails?

Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.

Where are environment variables stored Rails?

You store separate environment variables in config/development. rb , config/testing. rb and config/production. rb respectively.


1 Answers

In config/environments/, there are some configuration files that get executed based on what environment you're currently in. Try defining a constant in one of those.

like image 59
zenazn Avatar answered Nov 11 '22 00:11

zenazn