0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Constraints 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 route constraint match?
Given this Rails route with a constraint, what URL will match the route and what will be the value of params[:id]?
Ruby on Rails
Rails.application.routes.draw do
  get '/items/:id', to: 'items#show', constraints: { id: /^\d{3}$/ }
end
AURL '/items/123' matches, params[:id] = '123'
BURL '/items/12a' matches, params[:id] = '12a'
CURL '/items/1234' matches, params[:id] = '1234'
DURL '/items/abc' matches, params[:id] = 'abc'
Attempts:
2 left
💡 Hint
The constraint uses a regular expression to limit the :id parameter to exactly three digits.
📝 Syntax
intermediate
2:00remaining
Which route constraint syntax is correct?
Identify the correct way to apply a route constraint that only allows lowercase letters for the :username parameter.
Aget '/users/:username', to: 'users#show', constraints: { username: /[a-z]+/ }
Bget '/users/:username', to: 'users#show', constraints: username: '[a-z]+'
Cget '/users/:username', to: 'users#show', constraints: { username: '[a-z]+' }
Dget '/users/:username', to: 'users#show', constraints: { username: /[A-Z]+/ }
Attempts:
2 left
💡 Hint
Constraints expect a Regexp object, not a string.
🔧 Debug
advanced
2:00remaining
Why does this route constraint not work as expected?
Consider this route:
get '/products/:code', to: 'products#show', constraints: { code: /\d{4}/ }
Why does the URL '/products/12345' still match this route?
AThe constraint only checks the start of the string, so '12345' matches because it starts with '1234'.
BRails ignores regex constraints longer than 3 digits.
CThe regex matches any string containing four digits, so '12345' matches because it has '1234' inside.
DThe regex is not anchored, so it matches any substring with four digits, allowing longer strings.
Attempts:
2 left
💡 Hint
Regex without anchors can match substrings anywhere in the string.
state_output
advanced
2:00remaining
What is the value of params[:year] after this route matches?
Given this route:
get '/archive/:year', to: 'archive#show', constraints: { year: /\d{4}/ }
And the URL '/archive/2023', what is the value of params[:year] inside the controller?
A2023 (integer)
B'year'
C'2023'
Dnil
Attempts:
2 left
💡 Hint
Route parameters are always strings.
🧠 Conceptual
expert
2:00remaining
Which statement about route constraints is true?
Select the correct statement about how route constraints work in Rails.
ARoute constraints only accept regular expressions and cannot use custom Ruby objects.
BRoute constraints can use a class with a matches? method to apply complex logic.
CRoute constraints are evaluated after the controller action is called.
DRoute constraints can modify the params hash before the controller receives it.
Attempts:
2 left
💡 Hint
Think about how you can customize route matching beyond regex.