Scaffolding is a way to quickly put an Active Record class online by providing a series of standardized actions for listing, showing, creating, updating, and destroying objects of the class. These standardized actions come with both controller logic and default templates that through introspection already know which fields to display and which input types to use. Example:

 class WeblogController < ActionController::Base
   scaffold :entry
 end

This tiny piece of code will add all of the following methods to the controller:

 class WeblogController < ActionController::Base
   verify :method => :post, :only => [ :destroy, :create, :update ],
          :redirect_to => { :action => :list }

   def index
     list
   end

   def list
     @entries = Entry.find_all
     render_scaffold "list"
   end

   def show
     @entry = Entry.find(params[:id])
     render_scaffold
   end

   def destroy
     Entry.find(params[:id]).destroy
     redirect_to :action => "list"
   end

   def new
     @entry = Entry.new
     render_scaffold
   end

   def create
     @entry = Entry.new(params[:entry])
     if @entry.save
       flash[:notice] = "Entry was successfully created"
       redirect_to :action => "list"
     else
       render_scaffold('new')
     end
   end

   def edit
     @entry = Entry.find(params[:id])
     render_scaffold
   end

   def update
     @entry = Entry.find(params[:id])
     @entry.attributes = params[:entry]

     if @entry.save
       flash[:notice] = "Entry was successfully updated"
       redirect_to :action => "show", :id => @entry
     else
       render_scaffold('edit')
     end
   end
 end

The render_scaffold method will first check to see if you’ve made your own template (like "weblog/show.rhtml" for the show action) and if not, then render the generic template for that action. This gives you the possibility of using the scaffold while you’re building your specific application. Start out with a totally generic setup, then replace one template and one action at a time while relying on the rest of the scaffolded templates and actions.

Methods
Public Instance methods
scaffold(model_id, options = {})

Adds a swath of generic CRUD actions to the controller. The model_id is automatically converted into a class name unless one is specifically provide through options[:class_name]. So scaffold :post would use Post as the class and @post/@posts for the instance variables.

It’s possible to use more than one scaffold in a single controller by specifying options[:suffix] = true. This will make scaffold :post, :suffix => true use method names like list_post, show_post, and create_post instead of just list, show, and post. If suffix is used, then no index method is added.

     # File vendor/rails/actionpack/lib/action_controller/scaffolding.rb, line 87
 87:       def scaffold(model_id, options = {})
 88:         options.assert_valid_keys(:class_name, :suffix)
 89: 
 90:         singular_name = model_id.to_s
 91:         class_name    = options[:class_name] || singular_name.camelize
 92:         plural_name   = singular_name.pluralize
 93:         suffix        = options[:suffix] ? "_#{singular_name}" : ""
 94: 
 95:         unless options[:suffix]
 96:           module_eval "def index\nlist\nend\n", __FILE__, __LINE__
 97:         end
 98:         
 99:         module_eval "\nverify :method => :post, :only => [ :destroy\#{suffix}, :create\#{suffix}, :update\#{suffix} ],\n:redirect_to => { :action => :list\#{suffix} }\n\n\ndef list\#{suffix}\n@\#{singular_name}_pages, @\#{plural_name} = paginate :\#{plural_name}, :per_page => 10\nrender\#{suffix}_scaffold \"list\#{suffix}\"\nend\n\ndef show\#{suffix}\n@\#{singular_name} = \#{class_name}.find(params[:id])\nrender\#{suffix}_scaffold\nend\n\ndef destroy\#{suffix}\n\#{class_name}.find(params[:id]).destroy\nredirect_to :action => \"list\#{suffix}\"\nend\n\ndef new\#{suffix}\n@\#{singular_name} = \#{class_name}.new\nrender\#{suffix}_scaffold\nend\n\ndef create\#{suffix}\n@\#{singular_name} = \#{class_name}.new(params[:\#{singular_name}])\nif @\#{singular_name}.save\nflash[:notice] = \"\#{class_name} was successfully created\"\nredirect_to :action => \"list\#{suffix}\"\nelse\nrender\#{suffix}_scaffold('new')\nend\nend\n\ndef edit\#{suffix}\n@\#{singular_name} = \#{class_name}.find(params[:id])\nrender\#{suffix}_scaffold\nend\n\ndef update\#{suffix}\n@\#{singular_name} = \#{class_name}.find(params[:id])\n@\#{singular_name}.attributes = params[:\#{singular_name}]\n\nif @\#{singular_name}.save\nflash[:notice] = \"\#{class_name} was successfully updated\"\nredirect_to :action => \"show\#{suffix}\", :id => @\#{singular_name}\nelse\nrender\#{suffix}_scaffold('edit')\nend\nend\n\nprivate\ndef render\#{suffix}_scaffold(action=nil)\naction ||= caller_method_name(caller)\n# logger.info (\"testing template:\" + \"\\\#{self.class.controller_path}/\\\#{action}\") if logger\n\nif template_exists?(\"\\\#{self.class.controller_path}/\\\#{action}\")\nrender_action(action)\nelse\n@scaffold_class = \#{class_name}\n@scaffold_singular_name, @scaffold_plural_name = \"\#{singular_name}\", \"\#{plural_name}\"\n@scaffold_suffix = \"\#{suffix}\"\nadd_instance_variables_to_assigns\n\n@template.instance_variable_set(\"@content_for_layout\", @template.render_file(scaffold_path(action.sub(/\#{suffix}$/, \"\")), false))\n\nif !active_layout.nil?\nrender_file(active_layout, nil, true)\nelse\nrender_file(scaffold_path(\"layout\"))\nend\nend\nend\n\ndef scaffold_path(template_name)\nFile.dirname(__FILE__) + \"/templates/scaffolds/\" + template_name + \".rhtml\"\nend\n\ndef caller_method_name(caller)\ncaller.first.scan(/`(.*)'/).first.first # ' ruby-mode\nend\n", __FILE__, __LINE__
100:       end