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?
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.
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.
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.
And last but not least, any kind of discussion regarding Ruby on Rails documentation is very welcome on the rubyonrails-docs mailing list .
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With