Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variable returns nil

Here's how I set my ENV variable

#~/repos/project
export GA_USERNAME="[email protected]"

When I try to use ENV['GA_USERNAME] in my application, it returns nil. However, in the console it strangely prints the variable and then returns nil...

1.9.2-p320 :003 > puts ENV['GA_USERNAME']
[email protected]
=> nil 

Edit I don't think my ENV variables are persisting. When I restart my computer, variable is nil and needs to be set again.

like image 950
mnort9 Avatar asked Sep 08 '26 06:09

mnort9


1 Answers

A traditional way to set env vars for a server process is:

RAILS_ENV=production GA_USERNAME=foobar rails s

If you want to specify the env vars in a persistent way, like in a file, try figaro. The README tells you how to start, but the idea is:

  1. rails generate figaro:install
  2. edit config/application.yml (which the last command created)
  3. in your app, it will automatically set per-environment (production, development, test) variables

UPDATE

dotenv might be an even better and simpler approach:

  1. gem 'dotenv-rails', :groups => [:development, :test]
  2. put cross-environment key-value pairs in .env
  3. put env-specific pairs .env.test or .env.development

See their README for all the details.

like image 129
Seamus Abshere Avatar answered Sep 09 '26 20:09

Seamus Abshere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!