gespinha
gespinha

Reputation: 8487

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.

Upvotes: 8

Views: 13011

Answers (4)

Vasily  Bodnarchuk
Vasily Bodnarchuk

Reputation: 25294

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

Upvotes: 2

Pedro Ivan
Pedro Ivan

Reputation: 332

If you don't need the variable to be variating, you should put this into initializers or on a helper class. If you're looking for enviroment, you don't need it because it's the default.

Upvotes: 0

RAJ
RAJ

Reputation: 9747

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

Upvotes: 2

awendt
awendt

Reputation: 13633

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.

Upvotes: 2

Related Questions