Bird
Raised Fist0
Ruby on Railsframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using nested routes in a Rails application?
easy
A. To speed up database queries automatically
B. To group related resources and show their connection in URLs
C. To create routes without controllers
D. To avoid using RESTful conventions

Solution

  1. Step 1: Understand nested routes concept

    Nested routes group related resources in URLs to reflect their relationship, like a parent and its children.
  2. Step 2: Identify the purpose in URL structure

    This grouping helps keep URLs organized and meaningful, showing the connection between resources.
  3. Final Answer:

    To group related resources and show their connection in URLs -> Option B
  4. Quick Check:

    Nested routes = Group related resources [OK]
Hint: Nested routes show parent-child URL relationships clearly [OK]
Common Mistakes:
  • Thinking nested routes speed up queries
  • Believing nested routes remove need for controllers
  • Assuming nested routes ignore RESTful design
2. Which of the following is the correct syntax to define a nested route for comments inside posts in Rails?
easy
A. resources :comments do resources :posts end
B. resource :posts, :comments
C. resources :posts do resources :comments end
D. resources :posts, nested: :comments

Solution

  1. Step 1: Recall nested route syntax

    In Rails, nested routes are defined by placing resources :child inside a block of resources :parent.
  2. Step 2: Match syntax to example

    resources :posts do resources :comments end correctly nests comments inside posts using a block.
  3. Final Answer:

    resources :posts do resources :comments end -> Option C
  4. Quick Check:

    Nested routes syntax = block inside parent resource [OK]
Hint: Use block syntax: parent do child end [OK]
Common Mistakes:
  • Reversing parent and child resources
  • Using commas instead of blocks
  • Using singular resource instead of plural
3. Given the nested routes below, what is the URL helper for editing a comment with ID 5 belonging to post with ID 3?
resources :posts do
  resources :comments
end
medium
A. edit_post_comment_path(3, 5)
B. edit_comment_path(5)
C. edit_post_path(3, 5)
D. edit_comments_post_path(5, 3)

Solution

  1. Step 1: Understand nested URL helpers

    For nested routes, the helper includes both parent and child resource names and IDs, like edit_post_comment_path(post_id, comment_id).
  2. Step 2: Match IDs to parameters

    Post ID is 3, comment ID is 5, so the helper is edit_post_comment_path(3, 5).
  3. Final Answer:

    edit_post_comment_path(3, 5) -> Option A
  4. Quick Check:

    Nested helper = edit_parent_child_path(parent_id, child_id) [OK]
Hint: Nested helpers need parent ID first, then child ID [OK]
Common Mistakes:
  • Using only child ID in helper
  • Swapping parent and child IDs
  • Using plural instead of singular in helper
4. Identify the error in the following nested routes definition:
resources :authors do
  resources :books
resources :publishers
end
medium
A. Should use resource instead of resources for books
B. Cannot nest resources :publishers inside resources :authors
C. No error, syntax is correct
D. Missing end for the resources :authors block

Solution

  1. Step 1: Check block structure

    The resources :authors block opens but does not close before defining resources :publishers.
  2. Step 2: Confirm correct nesting

    Each block must have a matching end. Here, resources :publishers is outside the intended block but inside the authors block without closure.
  3. Final Answer:

    Missing end for the resources :authors block -> Option D
  4. Quick Check:

    Every block needs matching end [OK]
Hint: Count your do/end pairs carefully [OK]
Common Mistakes:
  • Forgetting to close blocks with end
  • Nesting unrelated resources incorrectly
  • Confusing singular and plural resource keywords
5. You want to create nested routes for projects and their tasks, but only allow index and show actions for tasks. Which is the correct way to define this in Rails?
hard
A. resources :projects do resources :tasks, only: [:index, :show] end
B. resources :projects do resources :tasks, except: [:index, :show] end
C. resources :projects, only: [:index, :show] do resources :tasks end
D. resources :projects do resource :tasks, only: [:index, :show] end

Solution

  1. Step 1: Use nested resources with action limits

    To limit actions on nested resources, use only: [:index, :show] inside the nested resources block.
  2. Step 2: Verify correct syntax

    resources :projects do resources :tasks, only: [:index, :show] end correctly nests tasks inside projects and limits actions to index and show.
  3. Final Answer:

    resources :projects do resources :tasks, only: [:index, :show] end -> Option A
  4. Quick Check:

    Limit nested actions with only: [OK]
Hint: Use only: to limit nested resource actions [OK]
Common Mistakes:
  • Using except instead of only
  • Limiting actions on parent instead of child
  • Using singular resource for multiple tasks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard