Understanding Rails · Season 2: Getting started
Your first scaffold, a guided tour
Episode 14 · overview · about 3 minutes · Rails 8.1 · by Tommy A. Caruso
How was this episode?
The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.
Not endorsed yet. Are you a Rails developer? What's an endorsement?
Endorse this version To endorse: create an account, confirm your email, then link GitHub.
What changed
- Version 2, November 20, 2026: Fixed the claim that every action answers JSON: new and edit answer HTML only.
Generate a scaffold, see its pages work, and tour the files it wrote, in the order a request uses them.
Free to watch. If it helped, support the series.
Transcript
0:00 Your first scaffold
Episode fourteen. Your first scaffold, a guided tour. One command writes a working feature: pages to list, add, change, and delete notes, with tests. Then we read the files it wrote.
0:16 The command
Here it is: bin slash rails generate scaffold Note, body colon text. Run db colon migrate, and the feature works.
0:26 What it wrote
It wrote sixteen files, and one line in routes dot rb. Four you've seen from generate model: the migration, the model, its test, and test data. The rest is new: a controller, its test, six views, a helper, and three JSON views.
0:42 The pages
Start the server and visit slash notes slash new. It's plain, with no styling, but it works. Type a note, press Create Note, and it's saved.
0:52 The route
Now read it, in the order a request travels. First the route: one line, resources notes, gives you the seven routes from episode three.
1:02 The migration
The migration creates a notes table with a body of type text. T dot timestamps adds created at and updated at, which Rails fills in for you.
1:13 The model
The model is two lines. Note inherits from ApplicationRecord, and that's enough: Active Record reads the table, and Note can find, save, and update notes.
1:24 before_action
The controller has seven actions. Before show, edit, update, and destroy, set note finds the note by its id, so those four don't repeat that step.
1:35 create
Create builds a note from note params, and tries to save it. If it saves, the browser is sent to the new note, with a notice. If it doesn't, the form is shown again, with status unprocessable content, and the errors.
1:50 HTML and JSON
Five of the actions answer in two formats: HTML for the browser, and JSON for other programs. New and edit are forms, so they're HTML only.
2:02 params.expect
At the bottom, note params uses params dot expect. Only the body may come through from the form. Anything else a request sends is left out.
2:12 Partials
The views share their parts. Underscore form is the form, used by both new and edit. Underscore note shows one note, used by index and show.
2:23 form_with
The form starts with form with, model note. For a new note, it posts to slash notes. For a saved one, it patches that note's own address. One form, two jobs.
2:35 Errors
Above the fields, the form lists every error that stopped a save. Note has no validations yet, so for now, every save succeeds.
2:44 The tests
And it wrote tests: seven, one for each action. Bin slash rails test runs them, and they pass from the start.
2:53 When to scaffold
Most real features are written by hand. But a scaffold is a quick start, and reading one shows you the Rails way, a file at a time.
Next: the console as a lab.
Check your understanding
-
The scaffold's form uses form_with(model: note). For a note that is already saved, where does the form send its data?
- POST /notes
- PATCH /notes/1
- GET /notes/1/edit
- PUT /notes/new
Show answer
PATCH /notes/1. form_with asks the record if it is persisted. A saved note is sent to its own address with PATCH. A new one is posted to /notes. Source: Relying on Record Identification, Action View Form Helpers (Rails Guides 8.1).
-
In the scaffold's create action, @note.save returns false. What does the browser get?
- A redirect to the new note, with a notice
- A redirect back to /notes
- The new form again, with status unprocessable content and the errors
- A blank page with status 500
Show answer
The new form again, with status unprocessable content and the errors. When the save fails, the HTML branch runs render :new with status unprocessable content, so the form shows again with its errors. Source: Scaffold controller template (jbuilder source, v2.15.1).
-
note_params is params.expect(note: [ :body ]). A request sends a body and also pinned: true. What reaches Note.new?
- Only the body
- The body and pinned
- Nothing: the whole request is rejected
- Only pinned
Show answer
Only the body. Keys you have not permitted are filtered out. Only body was listed, so pinned is dropped. Source: permit, Action Controller Overview (Rails Guides 8.1).
-
Right after the scaffold, you run bin/rails test. How many tests run?
- None, because the scaffold writes no tests
- Seven, one for each controller action
- Eight: seven controller tests and a system test
Show answer
Seven, one for each controller action. The scaffold writes a controller test for each of the seven actions. Since Rails 8.1, it no longer writes system tests by default. Source: Scaffold controller test template (Rails source, v8.1.4.1).
Sources
Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.
- Scaffold controller template (jbuilder source, v2.15.1)
- Scaffold controller test template (Rails source, v8.1.4.1)
- Railties CHANGELOG: scaffolds stop generating system tests (Rails source, v8.1.4.1)
- permit, Action Controller Overview (Rails Guides 8.1)
- Relying on Record Identification, Action View Form Helpers (Rails Guides 8.1)