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.
Related posts:







Common Rails command shortcuts http://bit.ly/bpFv13
This comment was originally posted on Twitter
Good trick!
Don’t know in the examples above, but at least for runner $@ should be quoted, like “$@” you know.
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” “$@”
}
Top Ruby Article: Common Rails command shortcuts: http://bit.ly/9Fkfrk
This comment was originally posted on Twitter
Hey Xavier, thanks for the improvements.
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.
Envy Lab’s @jswanner wrote up a short article this evening covering his Rails command line shortcuts. http://bit.ly/axRGhY
This comment was originally posted on Twitter
Common Rails command shortcuts http://ff.im/-nNsdT
This comment was originally posted on Twitter
Thanks for taking the time to share your tips!
[...] 译自:http://blog.envylabs.com/2010/07/common-rails-command-shortcuts/ [...]
[...] 译自:http://blog.envylabs.com/2010/07/common-rails-command-shortcuts/ 归类于:Rails 3 标签: [...]
our terminal project got a mention in the @envylabs blog! If you’re interested in helping we’d like to make a gem http://bit.ly/9u5JHm
This comment was originally posted on Twitter
This could probably also include a check to run “bundle exec whatever” if bundler is being used.