Useful Rails Commands

This file lists several common commands that you may find useful in the Ruby on Rails projects for CS142. Except for the rails and gem commands, you should run these commands in the top-level directory for a Rails application.

rails foo

Creates a new Rails applications in subdirectory foo.

ruby script/generate model foo

Creates a new model class Foo with a skeleton class definition in app/models/foo.rb and a skeletal migration in db/migrate/*_create_foos.rb.

ruby script/generate controller foo a b

Creates a new controller class FooController with a skeleton class definition in app/controllers/foo_controller.rb. It also creates skeleton action methods a and b in the controller, plus skeleton views in the files app/views/foo/a.html.erb and app/views/foo/a.html.erb. If a and b are omitted then the controller is created with no actions or views.

ruby script/generate migration foo

Creates a new migration in the file db/migrate/*_foo.rb.

rake db:migrate

Runs all migrations to bring the database up to date.

rake db:migrate:reset

Drops the database, creates a new one, and runs all migrations to bring the database up to date.

rake db:migrate VERSION=20090909201633_create_photos.rb

Runs migrations (either forward or backward) to restore the database to match the state just after the given migration.

ruby script/server

Starts up a Web server on port 3000 running the current application; log messages from the server will appear on standard output.

gem update

Updates all Ruby Gems.

gem update rails--include-dependencies

Updates Rails to the latest version, including all Gems that Rails depends upon.

gem update --system

Updates Ruby to the latest version.