« TutorialBasicRelational | Tutorial | TutorialHelperMethods »
Beware: This does not work anymore in Rails 2.0! Go to the bottom of the page for some details on doing it in 2.0
Rails provides a method for starting development on a controller, which quickly allows you to see data in your model and add, delete, show and list the model data.
This method is called scaffolding, which sets up a temporary framework for you to work around. You can then replace the initial methods with methods of your own.
This example assumes that you have already set up a database as described in TutorialBasicRelational. Below is the SQL schema in use for this example (it’s the same
as previously developed in the tutorial).
For this example to work properly, we must remove some of the methods defined in friends_controller.rb and their corresponding view rhtml files. If these methods and views are defined, they will override the ones that scaffolding will provide, defeating the purpose of this example. Later, when we are no longer satisfied with the methods and views provided by the scaffold, we can redefine them to override the defaults.
Edit app/controllers/friends_controller.rb and delete (or rename) the list, new, and edit methods.
Then delete (or rename) the files list.rhtml, new.rhtml, and edit.rhtml from the directory app/view/friends/
We will use the database that has been created so far from the preceeding steps of the tutorial.
For convenience, here is the sql covered to this point in the tutorial
CREATE TABLE `people` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(50) NOT NULL default '', `street1` varchar(70) NOT NULL default '', `street2` varchar(70) NOT NULL default '', `city` varchar(70) NOT NULL default '', `state` char(2) NOT NULL default '', `zip` varchar(10) NOT NULL default '', PRIMARY KEY (`id`), KEY `name` (`name`) ) TYPE=MyISAM AUTO_INCREMENT=5 ; INSERT INTO `people` VALUES (1, 'Superman', 'PO Box 1', '123 Somewhere', 'Smallville', 'KS', '123456'); # -------------------------------------------------------- # # Table structure for table `phones` # CREATE TABLE `phones` ( `id` int(10) unsigned NOT NULL auto_increment, `person_id` int(10) unsigned NOT NULL default '0', `phone` varchar(15) NOT NULL default '', PRIMARY KEY (`id`), KEY `people_id` (`person_id`), KEY `phone` (`phone`) ) TYPE=MyISAM AUTO_INCREMENT=3 ; # # Dumping data for table `phones` # INSERT INTO `phones` VALUES (1, 1, '6203423065'); INSERT INTO `phones` VALUES (2, 1, '6204811303');
In the previous steps in the tutorial, we have been very limited in the manipulation of our data. By simply adding a line of code to our controller, we can get some basic functionality that may help us during the initial development. Just add this into the controller class: friends_controller.rb)
scaffold :person
The methods are list, show, destroy, new, create, edit, and update. If you go to http://rails/friends/list show you the list method, and you can see and edit the data in the people table. Play around with it, look at the source to get a feel for how the URLS work (but we’ll see an easier way to manage that in a bit). Restarting the server may be required before these new methods are available.
If you did TutorialStepThree you’ll have an old list.rhtml and and a list controller that will mess stuff up, remove them to use the scaffold instead. /Raven
AjaxScaffold : a alternate starting point
Alternately you could use the AjaxScaffold which provides you with a much more polished, some might even say production ready, scaffold. And I think the documentation is a tad more concise than whatever is going on on this page. Check it out. I’m not so sure – none of the instructions work, shame as it looks great.

ActiveScaffold : is the follow up to AjaxScaffold.
This one at least has documentation. ;) Dry_scaffold has been deprecated in favor of this.
dry_scaffold : another alternate starting point
dry_scaffold also has a polished scaffold only DRY. Check it out.
Scaffold in Rails version 2.0
To get scaffold to work in Rails 2.0 you will need to run the command:
/script/generate scaffold Person name:string street1:string street2:string...
This will generate your migration, controller, views and model for you with the fields provided.
All you need to do then is run rake to generate your database table from the migration file.
rake db:migrate
« TutorialBasicRelational | Tutorial | TutorialHelperMethods »