How to Have Constants Available Throughout App?

I discovered that you can add these modules under /lib with constants in them and call these constants like so :

module AdminConstants

   email ='admin@test.com'    phone ="123456789"

end

and then access them using AdminConstants::email

sometimes I can put in a require "adminConstants" in order to access them. I am thinknig of putting them all over the place. Whereever there is any value hardcoded. Can I use this in development.rb?

How about if I wanted different set of constants for different environments?

like x=1;y=2 in development but x=2,y=3 in production?

Can I have the right files load during startup? Is it possible to initialize the constants in the lib modules with values from the database?

Why not add these to a YAML config file? Check out this Railscast #85 YAML Configuration File - RailsCasts

Best. Mike

You could put the constants in config/environment.rb:

if RAILS_ENV == “development” EMAIL = “admin@test.com” PASSWORD = “1234567890” else EMAIL = “admin@test.com” PASSWORD = “0987654321” end

And then just access them as EMAIL and PASSWORD in your application.

Ather Shiraz wrote:

How about if I wanted different set of constants for different environments?

like x=1;y=2 in development but x=2,y=3 in production?

Can I have the right files load during startup? Is it possible to initialize the constants in the lib modules with values from the database?

Here's what I do:

I have a file config/app_config.rb in which I have all of my settings in a hash. It looks like this (formatting might be messed up some):