Managing ENV variables in Rails

Managing ENV variables in Rails

___

In this tutorial, I will guide you on an easy method on how to set up environment config variables in Rails.

Often when developing Rails applications, you will find a need to setup a couple of environment variables to store secure information such as passwords, api keys, secrets, etc. This is done especially when your application's code is open source and you don't want people who access your code to see your passwords, secret keys etc. Setting this variables as environmental variables is safe as these values are stored at the operating system level that your app has access to.

There are many solutions out there like adding this keys in your .bashrc or .zrshrc, using gems such as Dotenv and Figaro. You can easily mess up system configuration files when using Bash or Zsh profiles. The Gems are great but if you want zero gem dependencies to manage your environment variables then I have a simple straight forward solution that I use myself when working on this kind of projects.

application.yml

Start of by creating a new file in your config directory of your rails app called application.yml. You can give it any name you like but in our case let's just call it so. You can then add all your keys in this file. You can even group them by environment. Here is an example

Loading Gist

Exporting Values

With this, we can export all this values as environmental variables by adding the following code in your application.rb file inside the config directory

Loading Gist

You can now access any values like this

Loading Gist

Conclusion

I think this is a much cleaner and dynamic way to manage your environmental variables. Remember to add the file (application.yml in our case) that contains your keys and secrets to your .gitignore file to avoid commiting it to version control. I'd be glad to hear what you think about this procedure in the comments section below. Thank you so much for reading. Happy hacking!!


4 Comments

___

E5fa00fe11a52176d8711b23e7c498c2dd8fe405

Benten Muiruri

20 Oct 14

Awesome Tutorial , I was always adding each of them to the application.rb file but your tutorial is a simple and elegant way to tackle it

0da049d15aa2c5981b79d8f1395a4f70fb161660

Yashi EL

09 Apr 15

Very nice,, thanks..

9180b864fb021f91e7f08e7bb4b11394fdef6994

chaimann

19 Jan 16

Thanks! One question - is it alright to do this all the same but without ENV? I mean, just to YAML.load file with keys into a CONFIG constant, and then access values like CONFIG["TWITTER_SECRET"]?

E8f640810ba0a79752dfd93b65143abfbc501d69

Ram

28 Jan 16

Yes Chaimann, I agree with you but in this case you load YML file once and make it accessible everywhere through environment variable. If we don't load to environment then you have load file whenever you want to use and access it through config object.

Latest Tutorials

___

Private Inbox System in Rails with Mailboxer New

Introduction It's been quite a while since my last tutorial and since then I've recieved alot of requests by email to implement a private messaging system ...

Ajax Sortable Lists Rails 4

With me, is a simple to-do list application where users can create dummy to-do lists and displays them in card-like form just like in Trello. We want to e...

Managing ENV variables in Rails

Often when developing Rails applications, you will find a need to setup a couple of environment variables to store secure information such as passwords, a...

Gmail Like Chat Application in Ruby on Rails

Introduction We are all fond of the Gmail and Facebook inline chat modules. About a week ago, I came across a tutorial on how to replicate hangouts chat...

Fast Autocomplete Search Terms - Rails

Introduction In many cases you find that you need to introduce a global search in your rails application i.e. search multiple models at a go using one form...

Load more scroll top