0
0
Ruby on Railsframework~20 mins

Nested routes in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Routes Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
end
A"/posts/5/comments/10"
B"/comments/10/posts/5"
C"/posts/10/comments/5"
D"/comments/5/posts/10"
Attempts:
2 left
💡 Hint
Remember nested routes include the parent resource's ID first.
📝 Syntax
intermediate
1:30remaining
Which nested route declaration is valid?
Which of the following nested route declarations is syntactically correct in Rails?
A
resources :posts
  resources :comments
end
B
resources :posts {
  resources :comments
}
C
resources :posts => {
  resources :comments
}
D
resources :posts do
  resources :comments
end
Attempts:
2 left
💡 Hint
Rails uses do...end blocks for nested routes.
state_output
advanced
2: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
end
ACommentsController#edit
BPostsController#edit
CPostsController#show
DCommentsController#show
Attempts:
2 left
💡 Hint
The URL ends with /edit on a nested resource.
🔧 Debug
advanced
2: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
end
Apost_comment_path is not defined for nested routes.
BMissing the post ID argument; nested routes require both post and comment IDs.
CThe routes file is missing a root route, causing all helpers to fail.
Dpost_comment_path expects only one argument, so two are needed.
Attempts:
2 left
💡 Hint
Nested routes require IDs for both parent and child resources.
🧠 Conceptual
expert
2: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
A10
B7
C14
D21
Attempts:
2 left
💡 Hint
Each resource generates 7 RESTful routes; nesting multiplies them.