Setting Local Environment Variables in Rails

As our RailsApps applications grow more popular, help requests are growing more frequent. Particularly with the release of the rails-stripe-membership-saas application, I’ve seen people having trouble with setting Unix environment variables. Granted, if you’re doing development with Rails, you really should be able to sort out your own problems with Unix environment variables. But telling newbies to school up is not really a solution. And sometimes the problems stem from rvm or other local shell complexities. Taylor Mock saw the problem and stepped up with solutions on two fronts. First, test suites to identify if the API keys required by the application are set properly; and second, a clever solution to set local environment variables through Rails, rather than the Unix shell.

Taylor Mock’s implementation takes advantage of Ruby’s ability to set environment variables as well as read them. We create a file named config/local_env.example.yml and then add some code to the Rails config/application.rb file to read key/value pairs from a YAML structure and set environment variables that can be used elsewhere in the Rails application. The result? A bulletproof way to set environment variables without the Unix shell.

Our approach was discussed on Hacker News today and several people suggested using the figaro gem which provides a similar implementation.

If you use the rails_apps_composer gem or the Rails Composer tool, there’s now an option to use the figaro gem and a config/application.yml file to set local environment variables.

For more about the solution, see the article Rails Environment Variables.

Special thanks to Taylor Mock this week. He’s making significant contributions to the RailsApps project with a combination of intelligence and dedication that’s overcoming obstacles and moving the project forward.

1 noteShowHide

  1. rails-apps posted this