What is Scaffolding in Ruby on Rails?

You may be learning Rails & you read that you have to create a “scaffold” to get your Rails application started…

Easy!

You can do this using the rails g scaffold command.

But what is scaffolding?

“Scaffolding is a temporary structure used to support a work crew to aid in the construction, maintenance and repair of buildings, bridges and all other man-made structures.” – Wikipedia

Translated into Rails:

A scaffold is a set of automatically generated files which forms the basic structure of a Rails project.

These files include:

  • A controller
  • A model
  • Views for every standard controller action (index, edit, show, new)

A new route.

And a migration to prepare your database.

Let’s see an example!

How to Use The Rails Scaffold Command

An example of scaffolding a project for a website about books would look like this.

rails g scaffold books

You should see a lot of text scrolling by, which details the files being created.

This example creates:

  • A BooksController
  • A Book model
  • A new resources :books route added to your config/routes.rb file
  • A set of testing-related files
  • View files under app/views/books (five in total)

Yes.

That’s a lot of stuff.

If you want to undo this scaffolding, right after creating it, you can use the following command.

rails d scaffold books

Where “d” means “destroy”.

Keep in mind that this will DELETE the files created by the scaffolding process.

Now:

Before you can use your scaffolded code you have to run your migrations to update your database schema.

Use the rails db:migrate command.

If no error messages show up, you’re ready! You have a basic structure for your new Rails application, or for a new feature that needs a new model, views & corresponding controller.

Next:

Run rails server.

Open your browser to localhost:3000/books & you should be able to see the results!

Rails Scaffolding With Extra Fields

By default…

Your model only gets timestamp fields, which means that the only information that you can record about your books (or whatever model you’re working with) is the time at which they were created, or updated.

Here’s how to scaffold with extra fields:

rails g scaffold books title:string author:string publication_year:integer

If you generate your scaffolding like this, you’ll have 3 fields to work with.

A title, an author, and a publication year.

That’s a bit more interesting than just having the database timestamps.

Btw.

This the same syntax we use for creating migrations with rails g migration.

Generating Specific Components

Scaffolding creates things you may not need or want right now.

But Rails is nice.

You can create individual components, like controllers, using the rails g (g for generate) command.

Examples:

  • rails g controller Fruit
  • rails g model Fruit name:string color:string (creates model + migration)
  • rails g migration CreateBook title:string year:integer (creates migration only)

One of the big benefits of using a scaffolding command is that all the files are created using the correct naming conventions, which avoids strange error messages. It also saves you the work of having to manually create these files.

Btw…

It’s considered good practice to delete auto-generated files that you don’t plan on using. So after using a generator like “g controller”, review the list of files created & remove those that you don’t need.

Summary

You’ve learned about scaffolding in Ruby so you can quickly jumpstart a new Rails application to practice with.

As you learn more you may want to let go of scaffolding.

But you can still generate individual controllers, models, or view files when you need them.

Thanks for reading! 🙂