Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a Rails environment variable locally (Mac OS X)

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="[email protected]"

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?

like image 969
Don P Avatar asked Feb 28 '14 18:02

Don P


2 Answers

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

export [email protected]

then do this in terminal

 source ~/.bashrc

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

like image 116
Paritosh Piplewar Avatar answered Sep 19 '22 23:09

Paritosh Piplewar


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

GMAIL_USERNAME = case Rails.env
  when 'development' then '[email protected]'
end

Restart your app and console.

You should be able to access it wherever you want:

> GMAIL_USERNAME
=> '[email protected]'
like image 32
Hesham Avatar answered Sep 18 '22 23:09

Hesham