Rails Scaffold Generator

Rails was designed with a focus on making the work of a developer easier. One of the ways it accomplishes this is by providing tools to automate core, commonly-used functionality. Generators encapsulate this idea, building out models, migrations, routes, and other assets by taking some parameters and quickly implementing our boilerplate code for us. This process is standardized which limits the potential for human error while also speeding up one of the more tedious aspects of developing a new project.

This can occasionally be a slippery slope as the convenience of these generators can tempt us to generate needless files, potentially confounding future collaborators with this unused filler. Generally, a developer should use the most lightweight generator that still meets her needs.

In this post, however, I want to focus on the heaviest of hitters: scaffold generators.

Getting Started

Typing or while in our project directory will give us all of our Rails generators:

Amongst the lighter-weight migration, model, and controller generators we can see our scaffold generator. Now, let’s take a closer look and see why this is such a powerful tool.

Rails Magic

In my coding bootcamp, we were allowed to use all of the generators except for scaffold during our Rails module. The reason for this is that scaffold generators take so much of the work out of the developer’s hands — a great thing for a more experienced developer but not the best idea for someone learning the Rails framework for the first time.

The basic format of the scaffold generator command is . So, what happens when we run ? Take a look:

That is a TON of stuff. Here’s a basic breakdown of what’s happening:

  • Created migration for a new database table with the attributes we supplied
  • Created accompanying model file that inherits from ApplicationRecord
  • Created controller file that inherits from ApplicationController, complete with basic CRUD methods as well as the private methods and
  • Created view directory which holds newly generated index, edit, show, new, and form template files
  • Created a file for controller styling
  • Created a route within the file with full resources
  • Uses Jbuilder to help write JSON in views
  • Added basic tests for models and controllers

The main differences between scaffold generator and resource generator come in the form of the view template files which are absent in the latter, as well as some magic in the controller. If you know you will have a resource that utilizes full CRUD, scaffold generator may be the way to go since it takes care of that chunk of boilerplate for you:

Again, if we have a controller that needs CRUD this saves us a bunch of time and limits room for errors that can arise when manually inputting this much code.

One final note: if you generate your Rails app with the API flag because you have a separate front end, you do not have to worry about Rails generating those View templates. It is smart enough to know that they are not needed for your project and will skip that step.

After previously fleshing this functionality out by hand, this sort of utility can almost seem like cheating. But you’ve earned your wings so go on and treat yourself with some scaffolding!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store