The following is a cheat sheet I created while going through Michael Hartl’s excellent RubyOnRails Tutorial (probably the best online resource out there for learning rails).

It may not make much sense to you, and these are only my raw notes with no explanation.

If you are serious about learning Rails, I recommend buying Michael’s book and creating your own.

> script/server

How to create a new page controller and view

http://railstutorial.org/chapters/modeling-and-viewing-users-one?version=2.3#sec:user_model

Generating a new pages controller

$ ruby script/generate controller Pages home contact

http://localhost:3000/controller/action
http://localhost:3000/pages/home

app/controllers/pages_controller.rb

class PagesController < ApplicationController
   def home
   end
   def contact
   end
end

app/views/pages/home.html.erb

<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>

To add another page update the controller and create the new view:

app/controllers/pages_controller.rb

class PagesController < ApplicationController
   def home
   end
   def contact
   end
   def about
   end
end

app/views/pages/about.html.erb

<h1>Pages#about</h1>
<p>Find me in app/views/pages/about.html.erb</p>

How to add a new resource

http://railstutorial.org/chapters/modeling-and-viewing-users-one?version=2.3#sec:user_model

$ script/generate rspec_controller Users new
exists  app/models/
     create  spec/models/
     create  spec/fixtures/
     create  app/models/user.rb
     create  spec/models/user_spec.rb
     create  spec/fixtures/users.yml
     create  db/migrate
     create  db/migrate/20101028132324_create_users.rb


db/migrate/_create_users.rb

class CreateUsers < ActiveRecord::Migration
   def self.up
     create_table :users do |t|
       t.string :name
       t.string :email

       t.timestamps
     end
end

   def self.down
     drop_table :users
   end
end

This is the database migration file. You can run ‘self’ and install the tables by running migrate.

$ rake db:migrate

Or you can roll back (and drop the table via down) by rolling back:

$ rake db:rollback

To add a column to our table in the future go:

$ script/generate migration add_password_to_users encrypted_password:string

This creates a new db migration:

db/migrate/_add_password_to_users.rb

class AddPasswordToUsers < ActiveRecord::Migration
def self.up
  add_column :users, :encrypted_password, :string
end

def self.down
  remove_column :users, :encrypted_password
end
end

Which you can then migrate:

$ rake db:migrate


Updates our database file db/development.sqlite3 which was created first time we ran db:migrate. This is our database.

Can view via the SQLLite manager pointing to this file.

Working at the console

http://railstutorial.org/chapters/modeling-and-viewing-users-one?version=2.3#sec:creating_user_objects

$ script/console –sandbox
$ tail -f log/development.log

<h2>Create new users</h2>

User.new – creates a new user in memory returns true/false
User.save – saves them to the database
User. create – creates and adds new user to database
User.destroy – removes user from database

>> User.new
>> user = User.new(:name => "Michael Hartl", :email => "mhartl@example.com")
>> user.save
>> user.name
>> foo = User.create(:name => "Foo", :email => "foo@bar.com")
>> foo.destroy

Finding users

>> User.find(1)
>> User.find_by_email("mhartl@example.com")
>> User.first
>> User.all

Updating users

>> user.email = "mhartl@example.net"
>> user.update_attributes(:name => "The Dude", :email => "dude@abides.org")
>> user.save
>> user.reload

Make it Restful

http://railstutorial.org/chapters/modeling-and-viewing-users-one?version=2.3#sec:a_users_resource

Active record isn’t restful by default. Need to enable by adding our resources to the routes table.

ActionController::Routing::Routes.draw do |map|
map.resources :users
.
.
.
end

Doing that, gives us this:

HTTP request URL Action Named route Purpose
GET /users index users_path page to list all users
GET /users/1 show users_path(1) page to show user with id 1
GET /users/new new new_user_path page to make a new user (signup)
POST /users create users_path create a new user
GET /users/1/edit edit edit_user_path(1) page to edit user with id 1
GET /users/1/edit edit edit_user_path(1) page to edit user with id 1
PUT /users/1 update user_path(1) update user with id 1
DELETE /users/1 destroy user_path(1) delete user with id 1