Rails 3 Cheat Sheet

Here are some quick tips for common Rails tasks.


Getting Started

To create a new application use the command:

rails new APP_NAME

Remember that if you want to create a template for building a Rails app, you can pass the rails new command a template using the -m or –template option.  To quickly create a template, be sure to check out the RailsWizard.

After you create an application, you’ll most likely want to test the result by starting the built-in server (WEBrick).

rails server

For those lazy programmers out there, you can just use rails s.

Back to top  


Rails has many built-in generators.  You can see the entire list by running the rails generate command without any options.  Remember that in Rails 3 you can use various shortcuts for commands.  With rails generate for example, you could just use rails g instead.  Here are a few common generators (the files created or changed are bolded in each example):


Generates a migration, a model, a controller, 5 views (index, show, edit, new, _form), a helper, unit test for the model, a fixture for the model, a functional test for the controller, a unit test helper, a stylesheet (scaffold.css), and finally it will add a resource route.

rails generate scaffold RESOURCE_NAME [field:type field:type] [options]

Scaffolding will get you up and running quickly, but it doesn’t always fit your particular applications.  Experienced developers typically choose to either avoid the scaffolding generator entirely or override the functionality with their own templates.


Generates a migration, a model, a unit test, and a fixture for the model

rails generate model MODEL_NAME [field:type field:type] [options]


Generates a migration for changing the database.

rails generate migration NAME [field:type field:type] [options]

Remember, if you give Rails a smart name, it will generate the correct code for you.  If your migration is in the form of add_xxxxx_to_yyyyy or remove_xxxxx_from_yyyyy and is followed by a list of columns and types, then the appropriate add_column and remove_column calls will be added automatically for you. You can also specify the name using UpperCamelCase such as AddXxxxToYyyy or RemoveXxxxFromYyyyy.


Generates a controller, functional test, a helper, and views for each of the actions you specify.

rails generate controller NAME [action action] [options]

If you use the controller generator and it is going to host a collection of resources, make sure your controller name is plural (e.g. notes, books, etc.).


A Rails mailer is similar to a controller but is used for sending out email.  It generates the mailer class, a functional test for the mailer, a fixture for testing, and views for each of the mails/methods that you specify.

rails generate mailer NAME [method method] [options]


Back to top  


Many of the generators create migrations for you, but you can build your own.  A migration is just a Ruby class designed to alter your database.  It consists of two methods, self.up to migrate your database, and self.down to rollback the migration.

By generating a migration, the generator is only creating a file, not altering the database structure.

To migrate your database (run all the pending migrations), use the command rake db:migrate.  This will run the self.up method within a migration.

To rollback a migration, use the command rake db:rollback.  This command will run the self.down method within a migration.

Back to top  


The best way to utilize gems in a project is by using Bundler.  Bundler works on a simple principle: you have a Gemfile within your project where you specify the gems that are required for your application/component.  In most cases (like within Rails), this will handle requiring that gem for you.  After you specify a gem, you need to tell Bundler that you made a change.  You can do this by issuing the command:

bundle install

By running a bundle install, Bundler will go out to whatever source you have specified in your Gemfile and will install any gems you do not have.  It also takes a snapshot of the gems and versions each time you run a bundle install and puts the information into the Gemfile.lock file.  By doing this, Bundler can quickly check if you have the appropriate gems on your system.  In addition, the Gemfile.lock is very helpful when deploying your application because it contains exact information about the required gems.

Back to top  

Useful Rake Tasks

We already looked at two rake tasks in the Migrations section above, rake db:migrate and rake db:rollback.

If you want to see the routes you have in your application, you can use the command rake routes and it will list the name of the route, the URI, the controller it will be routing to, and the specific action of the controller.

You can see all of the rake tasks available to you by issuing the rake -T command.

Back to top

Leave a Reply