📖 Scenario: You are building a simple blog application where each post can have many comments. You want to organize your routes so that comments are nested inside posts. This helps keep URLs clear and shows the relationship between posts and their comments.
🎯 Goal: Create nested routes in a Rails application so that comments are nested inside posts. This will generate URLs like /posts/:post_id/comments for comments related to a specific post.
📋 What You'll Learn
Create a resource route for posts
Create a nested resource route for comments inside posts
Use the Rails resources method for routing
Ensure the nested routes generate URLs with post_id as a parameter
💡 Why This Matters
🌍 Real World
Nested routes help organize URLs in web apps where one resource belongs to another, like comments belonging to posts.
💼 Career
Understanding nested routes is essential for Rails developers to build clear, maintainable URL structures and RESTful APIs.
Progress0 / 4 steps
1
Set up the posts resource route
In the config/routes.rb file, create a resource route for posts using resources :posts.
Ruby on Rails
Hint
Use resources :posts to create all standard routes for posts.
2
Add nested comments resource inside posts
Inside the resources :posts block, nest the comments resource by adding resources :comments indented inside.
Ruby on Rails
Hint
Indent resources :comments inside the resources :posts do ... end block.
3
Verify nested routes generate correct URL helpers
Check that the nested routes generate URL helpers like post_comments_path(post) and post_comment_path(post, comment) by running rails routes in the terminal.
Ruby on Rails
Hint
Use the terminal command rails routes to confirm the nested routes exist.
4
Add a root route for the application
Add a root route that points to posts#index by adding root 'posts#index' at the bottom of the routes.rb file.
Ruby on Rails
Hint
The root route sets the home page URL to show the posts index.
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
Step 1: Understand nested routes concept
Nested routes group related resources in URLs to reflect their relationship, like a parent and its children.
Step 2: Identify the purpose in URL structure
This grouping helps keep URLs organized and meaningful, showing the connection between resources.
Final Answer:
To group related resources and show their connection in URLs -> Option B
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
Step 1: Recall nested route syntax
In Rails, nested routes are defined by placing resources :child inside a block of resources :parent.
Step 2: Match syntax to example
resources :posts do
resources :comments
end correctly nests comments inside posts using a block.
Final Answer:
resources :posts do
resources :comments
end -> Option C
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
Step 1: Check block structure
The resources :authors block opens but does not close before defining resources :publishers.
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.
Final Answer:
Missing end for the resources :authors block -> Option D
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
Step 1: Use nested resources with action limits
To limit actions on nested resources, use only: [:index, :show] inside the nested resources block.
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.
Final Answer:
resources :projects do
resources :tasks, only: [:index, :show]
end -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.