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
Written by Adam Bird
Related protips
2 Responses
Simple, does not clutter your actual code. I like it.
over 1 year ago
·
thanks :)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Related Tags
#rails
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#