Last Updated: February 25, 2016
·
2.81K
· adambird

Environment variables in Rails development

Environment variables are generally the right place to store credentials and other config tokens that you don't want to store with your source code. Setting these for development purposes can be a bit of a coordination pain so I use the following technique.

Create a file in config/initializers called 01_development_env.rb that looks ilke this

if Rails.env.development?
  ENV['GOOGLE_CLIENT_ID'] = "..."
  ENV['ANOTHER_VARIABLE'] = "..."
  ...
end

When the Rails app starts up it'll load these along with the other initalizers. Note the 01_ file name prefix. Rails will run the initializers in ASCII name order.

Lastly add the file to the project's .gitignore file so that it doesn't get checked in.

$ echo "config/initializers/01_development_env.rb" >> .gitignore

2 Responses
Add your response

Simple, does not clutter your actual code. I like it.

over 1 year ago ·

thanks :)

over 1 year ago ·