8
votes

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.

4
Use dotenv gem github.com/bkeepers/dotenvRAJ
I fix your questions, and suggest your read Bash-Beginners-GuideЗелёный

4 Answers

2
votes

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.

2
votes

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

2
votes

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
0
votes

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.