So far, it\’s RoR

Ruby on Rails, PostgreSQL, SVN, etc….

  • Pages

  • Subscribe

  • September 2007
    M T W T F S S
    « Aug   Oct »
  • Advertisements

What exactly does the scaffold_resource generator give you?

Posted by Chirag Patel on September 25, 2007

The regular scaffold generator (e.g. ruby script/generate scaffold model controller) requires you to create your model and controller first. The scaffold_resource generator, on the other hand, generates the (RESTful) model and controller for you. For example:

ruby script/generate scaffold_resource Caregiver first_name:string last_name:string

In Rails 2.0, use resource instead:

ruby script/generate scaffold Caregiver first_name:string last_name:string

Here’s everything you get:

Views for all the CRUD operations and a layout

create app/views/caregivers/index.rhtml

create app/views/caregivers/show.rhtml

create app/views/caregivers/new.rhtml

create app/views/caregivers/edit.rhtml

create app/views/layouts/caregivers.rhtml


create app/models/caregiver.rb

Controller with methods for CRUD operations

create app/controllers/caregivers_controller.rb


create app/helpers/caregivers_helper.rb

Looks like this initially

module CaregiversHelper


Migration to create the table in the database

create db/migrate/003_create_caregivers.rb

Looks like this:

class CreateCaregivers < ActiveRecord::Migration

  def self.up

    create_table :caregivers do |t|

      t.column :first_name, :string

      t.column :last_name, :string

      t.column :address, :string

      t.column :city, :string

      t.column :state, :string

      t.column :home_phone, :string

      t.column :work_phone, :string

      t.column :cell_phone, :string

      t.column :relationship, :string

      t.column :email, :string


  end  def self.down

    drop_table :caregivers



Sets up route in config/routes.rb for REST

map.resources :caregivers


One Response to “What exactly does the scaffold_resource generator give you?”

  1. nice, thanks man

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: