0
0
Ruby on Railsframework~20 mins

Route parameters in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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?
AThe page will display the text '42'.
BThe page will display the text ':id'.
CThe page will show a routing error.
DThe page will display the text 'books#show'.
Attempts:
2 left
💡 Hint
Think about how route parameters are passed to the controller.
📝 Syntax
intermediate
2: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?
Aget '/profile/*username', to: 'users#show'
Bget '/profile/username', to: 'users#show'
Cget '/profile/:username', to: 'users#show'
Dget '/profile/:id', to: 'users#show'
Attempts:
2 left
💡 Hint
Route parameters use a colon before the name.
🔧 Debug
advanced
2: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?
ABecause the controller action 'edit' is missing.
BBecause the route should be defined as 'get '/items/edit/:item_id''.
CBecause the route should use a query parameter instead of a route parameter.
DBecause the route expects a segment between '/items/' and '/edit' to capture as :item_id, but '/items/edit' has none.
Attempts:
2 left
💡 Hint
Look at the URL structure and the route pattern carefully.
state_output
advanced
2: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?
Aparams[:author_id] is '5' and params[:id] is '10'.
Bparams[:author_id] is '10' and params[:id] is '5'.
Cparams[:author_id] and params[:id] are both '5'.
Dparams[:author_id] and params[:id] are both '10'.
Attempts:
2 left
💡 Hint
Nested resources pass the parent resource id with _id suffix.
🧠 Conceptual
expert
2: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?
AActionController::UrlGenerationError
BRoutingError
CNoMethodError
DActiveRecord::RecordNotFound
Attempts:
2 left
💡 Hint
Think about how Rails matches URLs to routes.