2

Just a quick question to which I couldn't find an answer on stackoverflow.

If it is easy to have environment variable for staging and production (on heroku for example), how can I set environment variable for my localhost (development environment)? (I am on a mac)

As of today I hardcode my api credential for development environment and I don't feel comfortable with that.

Thanks !

2
  • Have you seen stackoverflow.com/questions/11648620/…? Commented Jul 24, 2014 at 14:44
  • Thanks, I saw that but it doesn't fits my needs. Sorry :( Commented Jul 24, 2014 at 16:23

3 Answers 3

5

Use dotenv is intended to be used in development:

Add your application configuration to your .env file in the root of your project.

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

You may also add export in front of each line so you can source the file in bash. in .bashrc

export S3_BUCKET=YOURS3BUCKET
export SECRET_KEY=YOURSECRETKEYGOESHERE

Then access in rails app ENV['S3_BUCKET']

Sign up to request clarification or add additional context in comments.

Comments

1

Environment variables are best placed in your .bash_profile file which lives in your home directory on the Mac: /Users/you/.bash_profile. Open that file and add something like this to the end of it:

export MY_ENV_VAR=my_env_value

or

export MY_ENV_VAR="a string with spaces in it"

export is a shell command that sets environment variables. Your .bash_profile is a bash script that runs every time you open a new shell session (open a terminal window) and therefore your export commands will run and set the env vars.

Then they will be available in the ENV constant when you're in Ruby.

Comments

1

Edit /Users/your_user_name/.bash_profile and add there:

export RAILS_ENV=development

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.