During the past two months I have been reading constantly about Rails and
how I could get more productive when writing code and testing my apps.
There is a ton of information about those matters on the web and I'll try
to include as many articles as I could find useful to my knowledge building.
Disclaimer: This article is heavily inspired by Thoughtbot's
Vim for Rails Developers which I stumbled upon during browsing the
screencasts of codeschool.
Editor of choice (vim)
When you work from the command line and you use linux, your editor
preference comes down to two choices: vim and emacs. I started with vim
some time ago so I'll stick with it.
If you are new to vim read this cheatsheet to learn the basical
Start by installing pathogen.vim, a vim plugin manager:
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
Then add this to your vimrc:
From now on, every plugin that is compatible with pathogen can be simply
installed by cloning its repo at
An alternative for pathogen is vundle. Haven't used it but it
Probably the one most useful plugin when dealing with Rails projects.
Install it with:
git clone git://github.com/tpope/vim-rails.git ~/.vim/bundle/vim-rails
Browsing through the app
You can use
:RController foos and it will take you straight
app/controllers/foos_controller.rb. As you might guess, same
:RModel foo, etc. There is also tab completion so that
you can toggle between all models/controllers, etc.
Another useful command is
:find. Invoking it with a name foo, it first
searches for a model named foo. Tab completion is also your friend.
One other really cool feature is the go to file. Supposedly we have the
class Blog < ActiveRecord::Base
Placing the cursor on the articles word and pressing
gf vim opens
the article model. After saving your changes you can go back to the blog
model by pressing
Run your tests through vim
Running test is also a matter of a command. Say you are editing a specific
spec/test file. All you have to do is run
:Rake and the tests for that
particular file will be ran, without leaving your favorite editor :)
The supported commands are a lot and your best bet is to invoke
:help rails in vim and learn about them.
Be sure to also check vim-rails on github.
SnipMate implements snippet features in Vim. A snippet is like a template,
reducing repetitive insertion of pieces of text. Snippets can contain
placeholders for modifying the text if necessary or interpolated code for
git clone https://github.com/tomtom/tlib_vim.git
git clone https://github.com/MarcWeber/vim-addon-mw-utils.git
git clone https://github.com/garbas/vim-snipmate.git
git clone https://github.com/honza/vim-snippets.git
Writing a method
Reading the source code of snippets above let's see how we can create a
method. The snippet reads:
So, the snippet is named
def and in order to invoke it we must write
def and hit Tab. It then expands, placing the cursor in the highlited
method_name. This is what it looks like:
Once you start typing, method_name gets replaced with what you type. When
you finish, hit Tab again to go to the method body.
Now all you have to do is read the
ruby.snippet and find out what
snippets are supported.
vim-fugitive brings the power of git commands inside vim.
Install it with:
git clone git://github.com/tpope/vim-fugitive.git ~/.vim/bundle/vim-fugitive
Check out the github page for a list of commands and some interesting
Terminal multiplexer (tmux)
Again, here you have two options.
tmux. My first contact was
with screen but recently I decided to try tmux.
I won't go into any details but I highly reccomend watching Chris Hunt's
presentation Impressive Ruby Productivity with Vim and Tmux.
It's an awesome talk.
There is a great article I stumbled upon yesterday about some must have gems
for development, some of which I haven't tested. Here is what I got so far.
jazz_hands is basically a collection of gems that you get for free with
just one gem. It focuses on enhancing the rails console. It provides:
- Pry for a powerful shell alternative to IRB.
- Awesome Print for stylish pretty print.
- Hirb for tabular collection output.
- Pry Rails for additional commands (show-routes, show-models, show-middleware) in the Rails console.
- Pry Doc to browse Ruby source, including C, directly from the console.
- Pry Git to teach the console about git. Diffs, blames, and commits on methods and classes, not just files.
- Pry Remote to connect remotely to a Pry console.
- Pry Debugger to turn the console into a simple debugger.
- Pry Stack Explorer to navigate the call stack and frames.
- Coolline and Coderay for syntax highlighting as you type. Optional. MRI 1.9.3/2.0.0 only
Again, visiting the github page, you will get all the info you want.
There is an open issue and installation on ruby 2.1.2 is failing for now.
For the time being you can put the following in your Gemfile:
gem 'jazz_hands', github: 'nixme/jazz_hands', branch: 'bring-your-own-debugger'
rubocop is a tool which checks if your code conforms to the
ruby/rails community guidelines.
You can check the article I wrote
where I explain how to set it up and running.
railroady is a tool that lets you visualize how the models and the
controllers of your app are structured. Instructions on how to install it are
on the github page. You can check how it looks like on the
fedoraruby project I'm currently working on.
annotate generates a schema of the model and places it on top of the model.
It can also place it on top of your rspec files and the factories. It looks
# == Schema Information
# Table name: bugs
# id :integer not null, primary key
# name :string(255)
# bz_id :string(255)
# fedora_rpm_id :integer
# is_review :boolean
# created_at :datetime
# updated_at :datetime
# last_updated :string(255)
# is_open :boolean
There is a ton of useful tools out there and if you are new to rails
development you can easilly get lost. Rails has Two Default Stacks is
a nice read that sums it up. I will try to update this post as I find more
useful tools in my way.
I am mostly in favor of rspec because of its descriptive language and the great
support by other complement testing tools.
So, why capybara and not cucumber? I'm not an expert on neither of these tools
but from my understanding capybara is more focused on developers whereas
cucumber's human language mostly targets aplications where one talks to a
Guard watches files and runs a command after a file is modified. This allows
you to automatically run tests in the background, restart your development
server, reload the browser, and more.
It has nearly 200 plugins which provide different options as guard is not only
used for testing. The particular plugin for rspec is guard-rspec.
When you make the smallest change to a test and you hit save, guard will
run that particular test group again to see if it still passes.
I tend to invoke guard with
guard -c which runs the tests in a clear console
Read the guard wiki page which is comprehensive and also watch the
[guard railscast] to better understand it.
Quoting from What is ctags?:
Ctags generates an index (or tag) file of language objects found in
source files that allows these items to be quickly and easily located
by a text editor or other utility.
There are a bunch of different tools to create a tags file, but the most
common implementation is exuberant ctags which we will use.
It supports 41 programming languages and a handful of editors.
Install ctags via your package manager. It should be supported in all
For a rails project, in your application root directory you can run:
ctags -R --exclude=.git --exclude=log *
This searches recursively all files in the current directory, excludes
log directories and creates a
tags file under current
dir. You may want to add it to
.gitignore by the way.
Next, adding the following line to
sets the location of the tags file, which is relative to the current
You can move the above options in
~/.ctags, so in our case this will
So in future runs of ctags all you need to do is
ctags doesn't autogenerate, so each time you write code that is tagable,
you have to run the command again. If you are working in a git repository
be sure to checkout Tim Pope's Effortless Ctags with Git.
What this does is:
Any new repositories you create or clone will be immediately indexed
with Ctags and set up to re-index every time you check out, commit,
merge, or rebase. Basically, you’ll never have to manually run Ctags
on a Git repository again.
Say we have a file containing hundrends of lines. Inside a method you
see the below definition:
While you could search for these methods, you can save a few keystrokes
by simply getting the cursor on the line of the method to search and in
vim normal mode press
Ctrl + ] (control and right square bracket).
This should get you where the method is. Go back to where you were by
Ctrl + t.
Note: The usage of ctags isn't restricted only in the current file.
If a method in your file is inherited by another class, then searching
for it will jump in this particular file.
Wouldn't it be cool if we could search for methods in the Rails source
code? Here is where the power of ctags really excels. All you have to do
is tell ctags to also tag the rails source code.
First I cloned the rails repository into
git clone https://github.com/rails/rails.git vendor/rails
It should take less than a minute to download. You wouldn't want
the rails source code to be included in your git tree, so you simply
vendor/rails by adding it to
Lastly, create again the tags with
Now navigate with vim to one of your models that has for example the
has_many, place the cursor on it (or just on the same line)
Ctrl + ]. Pretty cool huh? In case you forgot, go back to where
you were with
Ctrl + t.
ack is like grep but on steroids.
Designed for programmers with large heterogeneous trees of source code,
ack is written purely in portable Perl 5 and takes advantage of the
power of Perl's regular expressions.
It supports multiple types which you can see by typong
Of course there is a vim plugin!
While reading the more-tools page of ack I found out about ag,
also called the_silver_searcher. It is said to search code about 3–5×
faster than ack, is written in C and have some more enhancements than ack.
You may want to give this a try to. And as you have guessed there is also
an ag vim plugin.
The editor of choice and the tools you use in web development play a great role
in one's productivity, so you have to choose wisely and spend some time to get
to know it. Personally, I learned a lot more these past days I was crafting
this post and I hope you got something out of it too :)