9

I created a new Rails app called sample_app and I use postgresql as my db (I already created a postgresql username and password). I use this setup guide https://gorails.com/setup/ubuntu/16.04

So I run this command rails new sample_app -d postgresql. And then I have to edit the config/database.yml to match the username and password to my postgresql's username and password I just created. But I don't want to hard-code because I will be using git.

I found this tutorial from digital ocean which suggest to use:

username: <%= ENV['APPNAME_DATABASE_USER'] %>
password: <%= ENV['APPNAME_DATABASE_PASSWORD'] %>

Is this the correct code? If so, since my app is called sample_app, my code should be?

username: <%= ENV['SAMPLE_APP_DATABASE_USER'] %>
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>

If this is not the correct one, can you help me? Thank you!

3
  • 1
    you can take any name for that, Commented Nov 3, 2016 at 12:54
  • can you expland? I don't understand what you mean, I'm really a newbie. Commented Nov 3, 2016 at 12:55
  • added an answer on how to set environment variables. pls check once. Commented Nov 3, 2016 at 13:05

3 Answers 3

13

There are many ways you can set the environment variables.

Here are two of them,

Option One: Setting ENV variables via a yml file

Create a file config/local_env.yml:

config/local_env.yml:

SAMPLE_APP_DATABASE_USER: 'your username'
SAMPLE_APP_DATABASE_PASSWORD: '******'

The above are the names you will use like,ENV['SAMPLE_APP_DATABASE_USER']. these can be names as your wish. you can take any name, but we should use the same name in the ENV reference.

add it to gitignore:

/config/local_env.yml

Change some code in application.rb

application.rb:

config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'local_env.yml')
  YAML.load(File.open(env_file)).each do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
end

The code opens the config/local_env.yml file, reads each key/value pair, and sets environment variables.

Using Environment Variables:

username: <%= ENV['SAMPLE_APP_DATABASE_USER'] %>
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>

Option Two: Use the Figaro Gem

The gem takes advantage of Ruby’s ability to set environment variables as well as read them. The gem reads a config/application.yml file and sets environment variables before anything else is configured in the Rails application.

Here’s how to use it. In your Gemfile, add:

gem 'figaro'

and run bundle install

The gem provides a generator:

$ bundle exec figaro install

The generator creates a config/application.yml file and modifies the .gitignore file to prevent the file from being checked into a git repository.

You can add environment variables as key/value pairs to config/application.yml:

SAMPLE_APP_DATABASE_USER: 'your username'
SAMPLE_APP_DATABASE_PASSWORD: '******'

The environment variables will be available anywhere in your application as ENV variables:

ENV["SAMPLE_APP_DATABASE_USER"]

Here are the remaining ways you can achieve the same.

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

7 Comments

I tried <%= ENV['SAMPLE_APP_USERNAME'] %> without creating another file and works perfectly. Maybe this means that I followed the setup guide correctly. To check, I tried hard-coding my username, it runs. Tried using wrong username, it failed to run. Tried the env variable, it run.
I'm using Ubuntu 16.04. I just followed the setup guide from gorails for Ubuntu 16.04 and followed your advice that I can just name it anything, and it works.
So I won't have to create another yml file.
good, in the sample application you might have followed one of the ways of setting the environment variables in the link I shared. that may not need a yml file
the two methods I have given are helpful if you deploy the project to live.
|
4

You can call it anything you want...

username: <%= ENV['CARROTS'] %>
password: <%= ENV['BEANS'] %>

You just have to make sure your deploy script sets the variables CARROTS and BEANS correctly.

3 Comments

What is a deploy script? I'm sorry, I'm really a newbie. I just followed the setup guide on this link gorails.com/setup/ubuntu/16.04
You need to google deployment script... it's how you deploy your code to a production environment.
How to Deploy a Rails 4 App With Git and Capistrano // Rob McLarty
2

try this gem dotenv-rails

add this to Gemfile:

gem 'dotenv-rails', :groups => [:development, :test]

bundle it. Now create a .env file on your apps's directory with following content:

SAMPLE_APP_DATABASE_USER: "devuser"
SAMPLE_APP_DATABASE_PASSWORD: "devuser"

restart the server you're good to go. these variables are exported when you boot your app which you can access in your database.yml file

username: <%= ENV['SAMPLE_APP_DATABASE_USER'] %>
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>

read dotenv-rails documentation for more info

Comments

Your Answer

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