Common Rails command shortcuts

7.15.10 by Jacob Swanner

I know many people — including myself — use Bash aliases for common command line tasks. Some popular examples would be: ss short for script/server, sc for script/console, and sg for script/generate.

These are setup by putting the following code in a Bash configuration file — such as ~/.bash_login:

alias ss='script/server'
alias sc='script/console'
alias sg='script/generate'

That works great, until you switch to Rails 3; all of these script files have been removed, and you use the rails command in their place: rails server instead of script/server, rails console instead of script/console, etc. Rails 3 provides shortcuts for the common commands: rails s is short for rails server, rails c is short for rails console, and so on. Some have suggested aliasing the rails command to just r, allowing you to use r s to start a server.

These shortcuts are nice, and is much less typing. But, I don’t want to have to remember when I’m in a Rails 2 app to use ss and then when I’m in a Rails 3 app to use r s; I want ss to just work in every project. Unfortunately, you cannot use a Bash alias to solve this problem; you can however use a Bash function! The following is a drop in replacement for the old alias ss=... stuff in your Bash configuration file:


function ss {
  if [ -e script/rails ]; then
    script/rails server $@
  else
    script/server $@
  fi
}
function sc {
  if [ -e script/rails ]; then
    script/rails console $@
  else
    script/console $@
  fi
}
function sg {
  if [ -e script/rails ]; then
    script/rails generate $@
  else
    script/generate $@
  fi
}

They work by checking for the existence of a script/rails file, which is new to Rails 3; if the file exists use it, otherwise fall back to using the Rails 2 version of the command; any arguments given to the function are passed along to the script, which is the $@ bit above.

I’ve contributed these changes to the Terminal project by Pigment, which includes similar shortcuts for many other common tasks.

  1. Xavier Noria says:

    Good trick!

    Don’t know in the examples above, but at least for runner $@ should be quoted, like “$@” you know.

  2. Xavier Noria says:

    A little refactor:

    function rails_command {
    local cmd=$1
    shift
    if [ -e script/rails ]; then
    script/rails $cmd “$@”
    else
    script/$cmd “$@”
    fi
    }

    function ss {
    rails_command “server” “$@”
    }

    function sc {
    rails_command “console” “$@”
    }

    function sg {
    rails_command “generate” “$@”
    }

    function sr {
    rails_command “runner” “$@”
    }

  3. Hey Xavier, thanks for the improvements.

  4. gregf says:

    I been using nginx under development for a while now, first mongrel, then passenger. I ended up creating a little function so I could easily start it and stop easily.

    # Start nginx and tail the logfile
    # catch ^C and kill nginx
    function ss {
    TRAPINT() {
    print “Caught Control C, shutting down nginx”
    sudo /etc/init.d/nginx stop
    }
    sudo /etc/init.d/nginx start
    tail -f log/development.log

    }

    Then I would just type ss under my rails application, it would launch nginx, and tail the log file for me. When I was done I could just hit ^C and it would automatically shut down nginx. I think passenger light is going to do away with needing this but may come in handy to someone.

  5. John Ranaudo says:

    Thanks for taking the time to share your tips!

  6. [...] 译自:http://blog.envylabs.com/2010/07/common-rails-command-shortcuts/ [...]

  7. [...] 译自:http://blog.envylabs.com/2010/07/common-rails-command-shortcuts/ 归类于:Rails 3   标签: [...]

  8. Richard says:

    This could probably also include a check to run “bundle exec whatever” if bundler is being used.

  9. [...] Common Rails command shortcuts « Envy Labs – September 16th %(postalicious-tags)( tags: bash rails alias terminal ruby rails3 rails2 tips tricks cli commandline )% September 16th, 2010, @ 5:00 am | Tags: links | Category: delicious links | Comments are closed | Trackback this Post | 0 views [...]

Leave a Reply

* Required Fields