Understanding Rails

Understanding Rails · Season 4: Controllers, routing and auth

Routing in depth

Episode 27 · deep dive · about 4 minutes · Rails 8.1 · by

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.

Was it clear?

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 nested and shallow routes tables, which left out the PUT route.

Nested and shallow routes, namespaces, constraints and concerns.

Free to watch. If it helped, support the series.

Transcript

0:00 Routing in depth

Episode twenty seven, and season four: controllers, routing and auth. We start with routing in depth. Episode three showed the router matching one request. Today: the shapes a routes file takes as an app grows.

0:18 resources

Resources notes draws eight routes. Index and create, on the plain path. New and edit, for the forms. Show, update, which answers both patch and put, and destroy. Bin rails routes, with dash c notes, lists just these.

0:34 only and except

Most resources don't need all of them. Only, index and show, draws just those two. Except works the other way round. Draw only the routes you'll build.

0:45 Nested routes

When notes belong to a notebook, nest them. Now every path carries the notebook's id: notebooks, notebook id, notes. The controller gets it in params, and can load the notebook first.

0:58 Too deep

But look at edit, show, update and destroy. A note's id is already unique. Repeating its notebook in those URLs makes them longer, and hands the controller two ids that have to agree.

1:11 shallow

Shallow true fixes that. Routes that act on the collection stay nested: index, create and new, because they need the notebook. Routes for one note drop it: slash notes, slash id. A good rule: nest one level at most, and keep member routes shallow.

1:28 member and collection

Not every action is one of the seven. Member adds a route for one record: patch publish, at notes, id, publish. Collection adds one for the whole list: get search, nested under the notebook. Before adding many of these, ask whether it's really a resource of its own.

1:45 Route helpers

Each route's name becomes a helper. Notebook notes path, given a notebook. Note path, given a note. Publish note path. Search notebook notes path, with a query string. The name is the prefix from bin rails routes, plus path, or url.

2:02 namespace

Namespace admin changes three things at once. The path starts with admin. The controller lives in an admin module. And the helper's name starts with admin.

2:14 scope

Scope changes just one. Scope module reports keeps the path at slash notes, but sends it to the reports notes controller. Scope slash archive changes only the path, and keeps the ordinary notes controller.

2:29 Constraints

Constraints narrow what a segment can match. Here, an id must be digits. Notes slash twelve is recognized as notes show. Notes slash abc matches no route at all, so it's a not found before any controller runs.

2:44 Routing concerns

Concerns share routes between resources. A commentable concern draws comment routes. Add it to notes and to notebooks, and both get comments, index and create, for the polymorphic comments from episode eighteen. Notice the digits rule came along, too, on note id.

3:03 Unused routes

As routes pile up, some end up pointing at nothing. Bin rails routes, dash dash unused, lists routes whose action doesn't exist. Here, all four comment routes, and notebook show. Ask the router for one, and Rails says a route matches, but references a missing controller.

3:21 The rules

So: draw only the routes you build. Nest one level, and keep members shallow. Use namespace for a separate area, and scope for a single change. And check for unused routes now and then.

3:34 Next

Next: strong parameters, and params expect.

Check your understanding

  1. Notes are nested in notebooks with shallow: true. Which path shows note 10?

    • /notebooks/7/notes/10
    • /notes/10
    • /notebooks/notes/10
    Show answer

    /notes/10. Shallow nesting keeps the collection routes (index, new, create) under the notebook, and gives routes for one note their own short path. Source: Routing: shallow nesting (Rails Guides 8.1).

  2. Inside resources :notes, you add member { patch :publish }. What path does it draw?

    • PATCH /notes/publish
    • PATCH /publish/notes/:id
    • PATCH /notes/:id/publish
    Show answer

    PATCH /notes/:id/publish. A member route acts on one record, so it carries the record's id: /notes/:id/publish, sent to notes#publish. Source: Routing: adding member routes (Rails Guides 8.1).

  3. The routes say resources :notes, constraints: { id: /\d+/ }. What happens with GET /notes/abc?

    • No route matches, so it's a not found before any controller runs
    • NotesController#show runs, and Note.find raises
    • Rails converts abc to 0
    Show answer

    No route matches, so it's a not found before any controller runs. A constraint is part of the match. abc isn't digits, so the route doesn't match at all. Source: Routing: segment constraints (Rails Guides 8.1).

  4. What does bin/rails routes --unused list?

    • Routes nobody has visited this week
    • Routes defined in config/routes.rb that no controller action uses
    • Routes with no name
    Show answer

    Routes defined in config/routes.rb that no controller action uses. An unused route is one in your routes file with no controller action behind it, like the comment routes before CommentsController existed. Source: Routing: listing existing routes (Rails Guides 8.1).

Sources

Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.

Comments

Sign in to comment or sign up.