Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading .env in Ruby on Rails application

I am setting up a heroku and I am using using this guide to set up my local environment for development. I have a '.env' file in the root of my application with the variable set up as noted in the guide. When I run

heroku local:run rails runner "puts ENV['S3_BUCKET']"

it returns a blank. How do I get it to recognize/load the '.env' file?

like image 437
npresco Avatar asked Dec 30 '15 19:12

npresco


People also ask

How to use environment variables in Ruby on rails?

There are several methods to use the environment variables in Ruby On Rails and also we have a gem like a Figaro Gem. If you are using the GitHub, to store and share code and your project is open source,it means any developer will have access to your code. You don’t want to share your private details or API keys with the public.

Is it possible to use ENV in a Ruby Application?

Don't use ENV for configuration within your Ruby application, that's NOT its purpose. ENV is only for reading external configuration coming outside of Ruby. Try a gem like dry-configurable for internal configuration options.

What is env in rails?

ENV ["RAILS_ENV"] defines the Rails environment (production, development, test, and so on) that Rails will run under. ENV ["RAILS_RELATIVE_URL_ROOT"] is used by the routing code to recognize URLs when you deploy your application to a subdirectory.

Where can I find discussion about Ruby on rails documentation?

And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome on the rubyonrails-docs mailing list .


1 Answers

I recommend using https://github.com/bkeepers/dotenv gem if you want to load .env file with ENV variables. If you are already using it then according to dotenv readme you should load as soon as possible on boot, for example in config/application.rb add:

require 'dotenv'
Dotenv.load

If you use PUMA or Unicorn, in config/puma.rb, config/unicorn.rb add:

  require "dotenv"
  Dotenv.load("/path/to/env/.env")
like image 198
midN Avatar answered Sep 28 '22 02:09

midN