Understanding Rails · Season 1: The trip of one click
Controllers
Episode 4 · overview · about 2 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 controller code so a failed save shows the form again instead of crashing the page.
Actions, before actions, params.expect, respond_to, the 422.
Free to watch. If it helped, support the series.
Transcript
0:00 Controllers
Part four. Controllers.
A controller is the conductor. It doesn't store data, and it doesn't draw pages. It asks the model for what it needs, then picks a view to answer with.
0:17 The notes controller
Here's the notes controller. It's one Ruby class, and each public method is an action. Ours is create.
0:25 Before actions
Before the action runs, some checks come first. Rails checks the authenticity token from part one. The app checks that you're signed in. And set trail, a before action, finds trail forty two.
0:38 params.expect
Next, note params. Never trust what a form sends. Params dot expect takes only the note's body, and drops anything else someone slipped in.
0:49 Saving the note
Then create builds a new note on the trail, sets its author to the signed in user, and asks it to save.
0:57 respond_to
If it saves, the controller answers in the format that was asked for. Remember the accept header from part one? Turbo asked for a turbo stream, so that's what it gets. A plain browser would be redirected instead.
1:11 When it doesn't save
If it doesn't save, the controller shows the page again with a four twenty two, the status for a request that was wrong, so the form can say what to fix.
Either way, the controller's part is short. The real decisions about a note belong to the model, and that's part five.
Check your understanding
-
NotesController has a public method create and a private method set_trail. Which one can a route send a request to?
- Both of them
- Only set_trail
- Only create
- Neither, until you list it in the controller
Show answer
Only create. Only public methods are callable as actions. Helpers like set_trail are kept private on purpose. Source: Controller Naming Convention, Action Controller Overview (Rails Guides 8.1).
-
The form sends a note with a body and also an author_id someone slipped in. What does params.expect(note: [ :body ]) give the controller?
- The body and the author_id
- Only the body
- Nothing, because of the extra field
- Only the author_id
Show answer
Only the body. Only the keys you list are permitted. Rails ignores any other parameters, so the extra author_id never reaches the model. Source: Strong Parameters, Getting Started with Rails (Rails Guides 8.1).
-
The controller says before_action :set_trail. When does set_trail run?
- Before the action, on each request
- After the view is rendered
- Once, when the server starts
- Only if the action calls it
Show answer
Before the action, on each request. Methods registered with before_action run before a controller action, and they can stop the request. Source: before_action, Action Controller Overview (Rails Guides 8.1).
-
The note fails to save. What does the controller send back?
- A redirect to the trail page, with status 302
- The trail page again, with status 422, so the form can show what to fix
- A turbo stream, with status 200
- An error page, with status 500
Show answer
The trail page again, with status 422, so the form can show what to fix. It renders the page again with 422. That status tells the browser the submission failed, and the form can show the errors. Source: Handling Errors, Getting Started with Rails (Rails Guides 8.1).
Sources
- Controller Naming Convention, Action Controller Overview (Rails Guides 8.1)
- before_action, Action Controller Overview (Rails Guides 8.1)
- expect, Action Controller Overview (Rails Guides 8.1)
- Strong Parameters, Getting Started with Rails (Rails Guides 8.1)
- Handling Errors, Getting Started with Rails (Rails Guides 8.1)