Using Scaffold With Existing Data

This is a premium course, which you can purchase below.

Buy Now

Scaffolding is a major productivity booster with new applications - but you can also use it to speed up your rewrite with existing data.


There are two ways to work with Rails' scaffolds. You can create one with a name and the fields that you need:

rails g scaffold customer first_name last_name email:uniq company address city state country
postal_code phone fax  --no-migration --no-jbuilder

And you can destroy what you've done by using destroy:

rails destroy scaffold customer

I created a scripts directory with two scaffold files in there as I found that I would create a scaffold, need to add some data or schema changes, and then redo it. This happened often, and so I made sure I knew what I was doing by recording the scaffolds in these tables.

Flexing Magical Rails Restful Routes

The "Big 7" in Rails are index show edit new create update delete. When you scaffold a resource, these routes (and their views) are generated for you, and all you need to do is alter them to suit your needs. This is mostly with the index and show routes, which the user will see (if you let them). The other routes are for admins to manipulate the data.