Scaffold: what one command generates

Free preview 2 min to read

Eight courses, one resource, built by hand

Getting Article from nothing to a working feature took eight courses. There was a migration (ActiveRecord: migrations), a model with a validation (ActiveRecord: model introduction), and a controller with index, show, new and create (ActionController: actions). Then came a safe article_params (Controller params & security), ERB views (ActionView: templates (ERB)), a reusable partial (ActionView: partials), a form_with model: @article form (Rails forms: form_with) and a resources :articles line (Rails routes: basics). Each piece was written by hand on purpose, so it could be understood on its own.

The command that skips all that

bin/rails generate scaffold Article title:string body:text published_at:datetime

One line. Run it once and every piece above exists: the migration, the model, a controller with all seven RESTful actions, the views and the routes entry, generated together.

Why "scaffold" is the right word

Construction scaffolding is a temporary structure, put up fast so workers can reach the real building and start on it. It isn't the building. Rails' scaffold generator borrows the name on purpose. What it produces is a real, runnable feature, but a generic one: a starting point a developer stands on and finishes, not a product to ship as it is. Lesson 6 covers what finishing it involves.

What gets generated

db/migrate/<timestamp>_create_articles.rb    the table
app/models/article.rb                         the model
app/controllers/articles_controller.rb        all 7 actions
app/views/articles/*.html.erb                 4 views and 2 partials
config/routes.rb                              resources :articles added
test/models/article_test.rb                   a model test
test/fixtures/articles.yml                    test data
test/controllers/articles_controller_test.rb  a controller test
app/helpers/articles_helper.rb                an empty helper

Lesson 2 runs that command in a disposable Rails app and shows that this list is what the generator itself prints as it runs.

What this course covers

  1. Running the generator in a scratch app and reading its terminal output.
  2. The generated migration and model, traced back to the courses that taught each by hand.
  3. The generated controller's seven actions: four familiar, three new.
  4. The generated views, and what "generic" looks like in generated ERB.
  5. When scaffolding earns its place, and what the generated code needs before shipping.
  6. The whole command, set against everything else these courses covered.

By the end, you can read every file rails generate scaffold produces against courses you've already finished.

Lesson 1 of 7 · free

Keep going with lesson 2

Running it in a scratch app

Runs the scaffold command in a disposable Rails app and reads the order of its internal generators.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...