Smarter rails server command


Some time ago I added the following line to my .zshrc (and .bashrc as well):

  alias rs='rails server -b 0.0.0.0'

This gave me an easy to use and easy to remember way to start my rails server when working on a Ruby on Rails project. I have several of these two-word aliases spread across my configuration files and I love them.

But recently I started working on a project that uses Foreman to run the server and several other niceties. It has a very nice README file that explains how to configure and run Foreman, but I never configured it until a couple of weeks ago.

My new command to start this particular app is

  foreman start

but I keep using rs since it's engraved in my muscle memory, and that's making me miss some awesome features of the development server.

So I finally decided to do it... I removed my rs alias from my config and created an rs command written in Bash that does what I need depending on the project.

Here's the script:

  #!/bin/bash

  if [ -f .foreman ]; then
      foreman start
  else
      rails s -b 0.0.0.0
  fi

It's tiny, but it helps big time. It checks if there is a .foreman file and in that case runs foreman start. If the file is not there, it means that Foreman is not configured on that particular project and runs the old rails s -b 0.0.0.0 instead.

Hope you like this trick.

Saluti.