When Should You Use Scaffolding?

By Michael Herman

Article by Michael Herman, author of Real Python for the Web, develops in Python during the day and in Ruby at Night. He doesn't sleep.

One year ago I never used Scaffolding, opting instead to build everything manually to fit the needs of my application. Having a greater understanding of Rails, I've learned to work with its assumptions rather than against them, and I've found that the Scaffolding is perfect for getting an application up quickly. In cases of standard applications, the scaffolding is perfect for getting an MVP up and running in little time at all, allowing you to focus on the core features of your application. Always remember, though, that the Scaffolding is just the means to another end.

Note: The example detailed in this post is based on an actual gig I completed a week ago.


Let's look at a quick example, using Ruby 2.0.0 with Rails 4.0.0, where we'll create an online shop. I'm operating happily in a Unix environment.


  1. Create a new project:

    $ rails new shop $ cd shop

  2. Generate the Scaffolding:

    $ rails generate scaffold product name 'price:decimal{7,2}'

  3. Since the model was created for us, push through the migration:

    $ rake db:migrate

  4. Create the seed by adding the following code to seeds.rb:

    Product.create( name: 'Create', price: 1.29) Product.create( name: 'Read', price: 1.13) Product.create( name: 'Update', price: 1.11) Product.create( name: 'Delete', price: 1.47)

    Yes, we are selling CRUD, in real life.

    Then populate the database:

    $ rake db:seed

The Routes

The Scaffold assumes we want to use the basic CRUD functions - e.g, create, read, update, and delete. So, it creates our URL routes to match those basic functions. Use the following command to see the available routes:

$ rake routes


Prefix     Verb   URI Pattern                  Controller#Action
 products    GET    /products(.:format)          products#index
             POST   /products(.:format)          products#create
 new_product GET    /products/new(.:format)      products#new
edit_product GET    /products/:id/edit(.:format) products#edit
     product GET    /products/:id(.:format)      products#show
             PATCH  /products/:id(.:format)      products#update
             PUT    /products/:id(.:format)      products#update
             DELETE /products/:id(.:format)      products#destroy

PATCH? What's that?

The question is simple: Do you want to allow all those actions? If so, leave the routes alone. If not, update routes.rb to meet the needs of your application.

The Controller

As you know, the routes invoke actions that are tied to the methods in the controller, products_controller.rb. Thus, the Scaffold creates the methods index(), show(), new(), create(), update() and destroy() automatically. This should all be straightforward. It even creates code for you to render the data in JSON for each method:

format.json { render :json => @products }

Now that's thoughtful!

If you deleted any routes make sure to also delete the corresponding methods in the controller.

The Views

You guessed it! The Scaffold creates views and templates on your behalf as well, including a base template, the templates for the main CRUD functions, and a partial template:

├── layouts
│   └── application.html.erb
└── products
    ├── _form.html.erb
    ├── edit.html.erb
    ├── index.html.erb
    ├── new.html.erb
    └── show.html.erb

Take a look at the code or fire up the server, rails s, to take a look at all the views. Go ahead and play around with the views, checking out each function. Delete any unneeded templates.

What's Next?

Either remove or restrict the methods (including the routes and templates/views) you don't want end users using. Add some styles. Make it pretty. Use Stripe to collect payments. Launch your MVP.

Get good at working both with and without the Scaffolding. Even if you are working with a standard application, which you will most likely use the Scaffolding with, there will be plenty of customization under the hood. The more you work without the Scaffolding the better you will get at development.