0
0
Ruby on Railsframework~10 mins

Nested routes in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested routes
Define parent resource
Define nested child resource inside parent
Rails generates URLs with parent and child IDs
Controller receives params with both IDs
Use params to find parent and child records
Render nested resource views
Nested routes let you organize URLs so child resources are inside parent resources, showing their relationship clearly.
Execution Sample
Ruby on Rails
Rails.application.routes.draw do
  resources :authors do
    resources :books
  end
end
Defines nested routes where books belong to authors, creating URLs like /authors/:author_id/books/:id
Execution Table
StepActionRoute GeneratedParams PassedController Action
1Define parent resource :authors/authors{}AuthorsController#index
2Define nested resource :books inside :authors/authors/:author_id/books{author_id}BooksController#index
3Access nested book show/authors/5/books/10{author_id: 5, id: 10}BooksController#show
4Access nested book edit/authors/5/books/10/edit{author_id: 5, id: 10}BooksController#edit
5ExitNo more routes definedN/AN/A
💡 All nested routes for books under authors are defined and mapped to controller actions
Variable Tracker
VariableStartAfter Step 2After Step 3Final
author_idnilset when URL accessed55
id (book id)nilnil1010
route_pathnil/authors/:author_id/books/authors/5/books/10/authors/5/books/10
Key Moments - 2 Insights
Why do nested routes include both :author_id and :id in params?
Because nested routes reflect the parent-child relationship, Rails passes both IDs so the controller knows which author and which book to find, as shown in execution_table step 3.
Can you access a book without specifying an author in nested routes?
No, nested routes require the parent resource ID in the URL, so you must specify :author_id to access books, as seen in the route patterns in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what controller action handles the URL /authors/5/books/10/edit?
AAuthorsController#edit
BBooksController#show
CBooksController#edit
DAuthorsController#show
💡 Hint
Check step 4 in the execution_table for the URL and corresponding controller action.
At which step does the route /authors/:author_id/books get defined?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Route Generated' column in execution_table for the nested books route.
If you remove the nested block and define resources :books separately, what changes in params?
AParams will no longer include :author_id
BParams will include both :author_id and :id
CParams will include only :author_id
DParams will include neither :author_id nor :id
💡 Hint
Check variable_tracker to see which params are set by nested routes.
Concept Snapshot
Nested routes in Rails:
- Use resources inside another resources block
- URLs include parent and child IDs
- Params include both :parent_id and :id
- Controllers use both IDs to find records
- Shows clear parent-child URL structure
Full Transcript
Nested routes in Rails let you organize URLs so child resources appear inside parent resources. For example, books nested inside authors create URLs like /authors/5/books/10. Rails passes both the author_id and book id in params so controllers can find the right records. The routes file uses resources :authors do resources :books end to define this. This structure helps show relationships clearly in URLs and controller logic.