Question

I'm trying to set environment variables in Rails. I'm following these docs: http://railsapps.github.io/rails-environment-variables.html

One option it gives is to save environment variables in your ~/.bashrc, using this syntax:

export GMAIL_USERNAME="myname@gmail.com"

I tried adding exactly this to my ~/.bashrc. Then I stop my rails server, close my terminal, open my terminal, start rails server. The environment variable still doesn't seem to be available.

I've checked if it is available by doing rails console in my project root folder, and trying > ENV["GMAIL_USERNAME"] # => outputs nil

How can I set an environment variable locally (in development) so that my Rails project has access to it?

Was it helpful?

Solution

I dont know which shell you are using. In case of bash , you can write this in your ~/.bashrc file

export GMAIL_USERNAME=abc@bah.com

then do this in terminal

 source ~/.bashrc

Now, check it console . I am sure it will be there .

OTHER TIPS

Create a new file: config/initializers/settings.rb

GMAIL_USERNAME = case Rails.env
  when 'development' then 'myname@gmail.com'
end

Restart your app and console.

You should be able to access it wherever you want:

> GMAIL_USERNAME
=> 'myname@gmail.com'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top