The Blog

Nov 29
0

Moving to Rails 3 Pain Point: rails server

by Blake Taylor

Update: I have posted a better solution!

Working with rails 2 and 3 projects back and forth, day to day has been pretty painless thanks to rvm and .rvmrc files. That is with the exception of accidentally running rails server on a rails 2 project. I do this all the time and it results in generating a new rails project called server rather then starting the rails server, my actual intention. It’s almost as frustrating when running script/server on a rails 3 project but at least it doesn’t spit out a bunch of useless files. Incidentally, all of the built in rails command (i.e. generate, console, …) set the same trap. To avoid falling prey, I have created a bunch of shell functions which I would like to share with everyone in case they would like to be privy as well.

The shell code is included in the gist below. I load these functions into my .bashrc file (actually my .zshrc file). Once they are are available just get in the habit of running rails-server or rails-generate or rails-whatever and the correct command will always be issued. Also, if anyone knows a better solution, do share!

As one final note, I run rails-server so often that I have is aliased to s. If you would like to do the same add alias s=rails-server to your .bashrc file.

# Functions to start rails server, run generate open console
rails-server () {
  if [ -e script/rails ]; then
rails server $*
  elif [ -e script/server ]; then
    ./script/server $*
  else
echo "Error: Not a rails app."
  fi
}

rails-console () {
  if [ -e script/rails ]; then
rails console $*
  elif [ -e script/console ]; then
    ./script/console $*
  else
echo "Error: Not a rails app."
  fi
}

rails-generate () {
  if [ -e script/rails ]; then
rails generate $*
  elif [ -e script/generate ]; then
    ./script/generate $*
  else
echo "Error: Not a rails app."
  fi
}

rails-about () {
  if [ -e script/rails ]; then
rails about $*
  elif [ -e script/about ]; then
    ./script/about $*
  else
echo "Error: Not a rails app."
  fi
}

rails-dbconsole () {
  if [ -e script/rails ]; then
rails dbconsole $*
  elif [ -e script/dbconsole ]; then
    ./script/dbconsole $*
  else
echo "Error: Not a rails app."
  fi
}

rails-performance () {
  if [ -e script/rails ]; then
rails performance $*
  elif [ -e script/performance ]; then
    ./script/performance $*
  else
echo "Error: Not a rails app."
  fi
}

rails-process () {
  if [ -e script/rails ]; then
rails process $*
  elif [ -e script/process ]; then
    ./script/process $*
  else
echo "Error: Not a rails app."
  fi
}

rails-destroy () {
  if [ -e script/rails ]; then
rails destroy $*
  elif [ -e script/destroy ]; then
    ./script/destroy $*
  else
echo "Error: Not a rails app."
  fi
}

rails-plugin () {
  if [ -e script/rails ]; then
rails plugin $*
  elif [ -e script/plugin ]; then
    ./script/plugin $*
  else
echo "Error: Not a rails app."
  fi
}

rails-runner () {
  if [ -e script/rails ]; then
rails runner $*
  elif [ -e script/runner ]; then
    ./script/runner $*
  else
echo "Error: Not a rails app."
  fi
}
view raw rails.sh This Gist brought to you by GitHub.
  • Reddit
  • Digg
  • del.icio.us
  • Facebook
  • Tumblr
  • Twitter

Leave a Reply

*