Challenge - 5 Problems
Route Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Rails route helper?
Given the route
get '/books/:id', to: 'books#show' and the controller action def show; render plain: params[:id]; end, what will be the output when visiting /books/42?Attempts:
2 left
💡 Hint
Think about how route parameters are passed to the controller.
✗ Incorrect
The route captures the segment after '/books/' as the :id parameter. The controller action renders this value, so visiting '/books/42' shows '42'.
📝 Syntax
intermediate2:00remaining
Which route definition correctly captures a username parameter?
You want to define a route that matches URLs like
/profile/johndoe and passes 'johndoe' as a parameter named username. Which route definition is correct?Attempts:
2 left
💡 Hint
Route parameters use a colon before the name.
✗ Incorrect
Using ':username' captures that part of the URL as params[:username]. Option C treats 'username' as a fixed string, A captures a wildcard but not as a named parameter, and D uses a different parameter name.
🔧 Debug
advanced2:00remaining
Why does this route not capture the parameter?
Given the route
get '/items/:item_id/edit', to: 'items#edit', why does visiting /items/edit cause a routing error?Attempts:
2 left
💡 Hint
Look at the URL structure and the route pattern carefully.
✗ Incorrect
The route expects a URL like '/items/123/edit' where '123' is the :item_id. Visiting '/items/edit' misses that segment, so Rails cannot match the route.
❓ state_output
advanced2:00remaining
What is the value of params in this nested route?
Given the nested route
resources :authors do resources :books end and visiting /authors/5/books/10, what will be the values of params[:author_id] and params[:id] in the BooksController?Attempts:
2 left
💡 Hint
Nested resources pass the parent resource id with _id suffix.
✗ Incorrect
In nested routes, the parent resource id is passed as params[:author_id], and the nested resource id is params[:id]. So visiting '/authors/5/books/10' sets author_id to '5' and id to '10'.
🧠 Conceptual
expert2:00remaining
What error occurs if a required route parameter is missing?
If a Rails route is defined as
get '/orders/:order_id/items/:id', to: 'items#show' and a request is made to /orders/15/items (missing the :id), what error will Rails raise?Attempts:
2 left
💡 Hint
Think about how Rails matches URLs to routes.
✗ Incorrect
Rails cannot match the URL '/orders/15/items' to the route requiring both :order_id and :id, so it raises a RoutingError indicating no route matches.