Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Rails server with environment variables set?

I have the following command. I insert it on starting the rails server

VARIABLE=development rails s

How do I make this variable start automatically, without having to wright it myself every time?

So I would then just do this

rails s

and it would run automatically with that variable.

like image 620
gespinha Avatar asked Jul 15 '15 09:07

gespinha


3 Answers

You have various options to do so:

  1. Declare your variables in .bashrc file and reload it.

  2. Use dotenv gem.

  3. Use figaro gem.

More info @ http://www.gotealeaf.com/blog/managing-environment-configuration-variables-in-rails

like image 155
RAJ Avatar answered Oct 19 '22 10:10

RAJ


Bundle the dotenv-rails gem, then create a .env file with the content:

VARIABLE=development

The gem picks up all variables from the file and sets them in your environment.

like image 2
awendt Avatar answered Oct 19 '22 08:10

awendt


Solution

./.env

APP_PORT=3000
APP_HOST=0.0.0.0
ENV=development

DATABASE_NAME=batman
DATABASE_USER=bat
DATABASE_PASSWORD=1234
DATABASE_PORT=5443

./start.sh

script that will run your rails app with all variables from ./.env

set -o allexport
source .env
set +o allexport
bundle exec rails s -p $APP_PORT -b $APP_HOST -e $ENV

set command info

Do not forget to set access privileges to execute ./start.sh

chmod +x ./start.sh

chmod command info

Usage

just run command from the root folder of your rails app:

./start.sh
like image 2
Vasily Bodnarchuk Avatar answered Oct 19 '22 08:10

Vasily Bodnarchuk