Understanding Rails · Season 1: The trip of one click
The router
Episode 3 · 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: A review against Rails 8.1 found the routes table left out the PUT route. It's there now.
routes.rb, resources, REST, nested routes, params, bin/rails routes.
Free to watch. If it helped, support the series.
Transcript
0:00 The router
Part three. The router.
The router has one job. It looks at the verb and the path, and decides which piece of code should answer.
0:14 routes.rb
Its instructions live in one file: config slash routes dot r b. Here are Switchback's.
0:21 Seven routes from one line
One line, resources trails, quietly makes seven routes. One for each thing you might do with trails: list them, show one, the form for a new one, create it, edit, update, and delete.
0:33 REST
This pattern has a name: REST. The verbs from part one, meeting the nouns of your app. Get a trail. Post a note. Delete a note.
0:43 Nested routes
Notes belong to trails, so their routes nest inside. That's how our click's path, slash trails slash forty two slash notes, finds its way.
0:52 Matching a route
The router reads its list from top to bottom, and the first route that matches the verb and the path wins. Post, trails, forty two, notes: that's notes, create.
1:04 Params
The forty two isn't thrown away. The router keeps it as the trail ID, and puts it in params, along with the note from the form.
1:13 bin/rails routes
Lost? Run bin slash rails routes, and Rails prints every route it knows. The names on the left become helpers, like trail notes path, so your code never types a URL by hand.
The router has its answer: the notes controller, and its create action. Rails builds that controller, and hands it the request.
Check your understanding
-
config/routes.rb has resources :trails. Which controller action answers PATCH /trails/42?
- trails#edit
- trails#update
- trails#create
- trails#show
Show answer
trails#update. resources maps PATCH on one trail's path to update. edit is the GET that shows the form. Source: CRUD, Verbs, and Actions, Rails Routing from the Outside In (Rails Guides 8.1).
-
The route is POST /trails/:trail_id/notes. A request comes in for POST /trails/42/notes. Where does the 42 end up?
- It is thrown away once the route matches
- In params[:id]
- In the request body, with the note
- In params[:trail_id], as the string "42"
Show answer
In params[:trail_id], as the string "42". Each named segment of the path is kept in params under its name. Here that name is trail_id. Source: Dynamic Segments, Rails Routing from the Outside In (Rails Guides 8.1).
-
resources :trails comes first in routes.rb, and get "trails/featured" comes after it. Where does GET /trails/featured go?
- trails#show, with "featured" as the id
- The featured route, because it is more exact
- Nowhere: Rails raises an error for two matches
- trails#index
Show answer
trails#show, with "featured" as the id. Routes are matched in the order they are written, and the first match wins. /trails/:id matches first. Source: CRUD, Verbs, and Actions, Rails Routing from the Outside In (Rails Guides 8.1).
-
You want to see every route your app knows, with the name each helper is built from. What do you run?
- bin/rails middleware
- bin/rails routes
- bin/rails console
- bin/rails db:migrate
Show answer
bin/rails routes. bin/rails routes lists every route in the order they appear in routes.rb, with its name, verb, path and action. Source: Listing Existing Routes, Rails Routing from the Outside In (Rails Guides 8.1).