Challenge - 5 Problems
Nested Routes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What URL does this nested route generate?
Given the following Rails routes configuration, what is the URL helper output for
post_comment_path(5, 10)?Ruby on Rails
Rails.application.routes.draw do
resources :posts do
resources :comments
end
endAttempts:
2 left
💡 Hint
Remember nested routes include the parent resource's ID first.
✗ Incorrect
In nested routes, the URL includes the parent resource ID first, then the nested resource ID. So post_comment_path(5, 10) generates /posts/5/comments/10.
📝 Syntax
intermediate1:30remaining
Which nested route declaration is valid?
Which of the following nested route declarations is syntactically correct in Rails?
Attempts:
2 left
💡 Hint
Rails uses do...end blocks for nested routes.
✗ Incorrect
Rails nested routes use do...end blocks. Curly braces or hash rockets are not valid here.
❓ state_output
advanced2:00remaining
What controller and action does this nested route call?
Given this nested route and a GET request to
/posts/3/comments/7/edit, which controller and action will handle the request?Ruby on Rails
Rails.application.routes.draw do
resources :posts do
resources :comments
end
endAttempts:
2 left
💡 Hint
The URL ends with /edit on a nested resource.
✗ Incorrect
The URL /posts/3/comments/7/edit targets the edit action of the CommentsController because comments are nested inside posts.
🔧 Debug
advanced2:00remaining
Why does this nested route URL helper raise an error?
Given this routes file, why does calling
post_comment_path(10) raise an error?Ruby on Rails
Rails.application.routes.draw do
resources :posts do
resources :comments
end
endAttempts:
2 left
💡 Hint
Nested routes require IDs for both parent and child resources.
✗ Incorrect
Because comments are nested inside posts, the helper post_comment_path requires two IDs: the post ID and the comment ID. Passing only one ID causes an argument error.
🧠 Conceptual
expert2:30remaining
How many routes are created by this nested resources declaration?
How many distinct routes are generated by this nested route declaration?
resources :authors do resources :books end
Attempts:
2 left
💡 Hint
Each resource generates 7 RESTful routes; nesting multiplies them.
✗ Incorrect
Each resources generates 7 routes (index, show, new, create, edit, update, destroy). Nesting books inside authors creates 7 routes for authors plus 7 nested routes for books, totaling 14.