Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using route constraints in a Rails application?
easy
A. To style the HTML output of views
B. To speed up database queries automatically
C. To manage user sessions and cookies
D. To limit which URLs match routes based on specific patterns or conditions

Solution

  1. Step 1: Understand what route constraints do

    Route constraints restrict which URLs can match a route by applying conditions like regex or subdomain checks.
  2. Step 2: Compare options to the purpose of constraints

    Only To limit which URLs match routes based on specific patterns or conditions describes limiting URL matches based on patterns or conditions, which is the core use of route constraints.
  3. Final Answer:

    To limit which URLs match routes based on specific patterns or conditions -> Option D
  4. Quick Check:

    Route constraints = limit URL matches [OK]
Hint: Route constraints control which URLs reach your routes [OK]
Common Mistakes:
  • Confusing route constraints with database or view logic
  • Thinking constraints manage sessions or cookies
  • Assuming constraints style HTML output
2. Which of the following is the correct syntax to add a regex constraint for a route parameter :id that only allows digits?
easy
A. get '/items/:id', to: 'items#show', constraints: { id: /\d+/ }
B. get '/items/:id', to: 'items#show', constraints: { id: '[a-z]+' }
C. get '/items/:id', to: 'items#show', constraints: { id: /[a-z]+/ }
D. get '/items/:id', to: 'items#show', constraints: { id: /\w+/ }

Solution

  1. Step 1: Identify regex for digits only

    The regex /\d+/ matches one or more digits, which fits the requirement.
  2. Step 2: Check syntax for constraints hash

    The syntax constraints: { id: /\d+/ } correctly applies the regex constraint to the :id parameter.
  3. Final Answer:

    get '/items/:id', to: 'items#show', constraints: { id: /\d+/ } -> Option A
  4. Quick Check:

    Regex for digits = /\d+/ [OK]
Hint: Use /\d+/ regex for digit-only route params [OK]
Common Mistakes:
  • Using string instead of regex for constraints
  • Using wrong regex like /[a-z]+/ for digits
  • Missing double backslash escape in regex
3. Given the route definition below, what URL will match the route?
get '/profile/:username', to: 'users#show', constraints: { username: /[a-z]{3,}/ }
medium
A. /profile/alex
B. /profile/Jo
C. /profile/123
D. /profile/Alice

Solution

  1. Step 1: Understand the regex constraint

    The regex /[a-z]{3,}/ means the username must be at least 3 lowercase letters.
  2. Step 2: Check each URL against the regex

    "/profile/Jo" fails because "Jo" has uppercase and only 2 letters.
    "/profile/alex" matches because "alex" is lowercase and 4 letters.
    "/profile/123" fails because digits are not allowed.
    "/profile/Alice" fails because of uppercase 'A'.
  3. Final Answer:

    /profile/alex -> Option A
  4. Quick Check:

    Regex requires lowercase 3+ letters = /profile/alex/ [OK]
Hint: Match regex carefully: lowercase letters only, length matters [OK]
Common Mistakes:
  • Ignoring case sensitivity in regex
  • Assuming digits match letter regex
  • Not counting minimum length in regex
4. Identify the error in this route constraint code snippet:
get '/orders/:order_id', to: 'orders#show', constraints: { order_id: '\\d+' }
medium
A. The route path is missing a leading slash
B. The controller action is incorrect
C. The regex is a string, it should be a Regexp object
D. The constraints hash key should be a symbol, not a string

Solution

  1. Step 1: Check the constraints value type

    The value '\\d+' is a string, but Rails expects a Regexp object for regex constraints.
  2. Step 2: Confirm correct regex syntax

    It should be /\d+/ (a Regexp literal), not a string '\\d+'.
  3. Final Answer:

    The regex is a string, it should be a Regexp object -> Option C
  4. Quick Check:

    Regex constraints need Regexp objects, not strings [OK]
Hint: Use /regex/ not 'string' for route constraints [OK]
Common Mistakes:
  • Using string instead of Regexp for constraints
  • Confusing route path syntax
  • Misnaming controller or action
5. You want to restrict a route to only respond when the subdomain is 'admin'. Which of the following route definitions correctly applies this constraint?
hard
A. get '/dashboard', to: 'admin#dashboard', constraints: { subdomain: 'admin' }
B. get '/dashboard', to: 'admin#dashboard', constraints: lambda { |req| req.subdomain == 'admin' }
C. get '/dashboard', to: 'admin#dashboard', constraints: { domain: 'admin' }
D. get '/dashboard', to: 'admin#dashboard', constraints: { subdomain: /admin/ }

Solution

  1. Step 1: Understand subdomain constraints options

    Rails allows constraints as regex, hash, or custom objects like lambdas for complex logic.
  2. Step 2: Evaluate each option

    get '/dashboard', to: 'admin#dashboard', constraints: { subdomain: 'admin' } uses a string for subdomain, which is not valid.
    get '/dashboard', to: 'admin#dashboard', constraints: { subdomain: /admin/ } uses regex /admin/, which matches subdomain 'admin' but may match partials.
    get '/dashboard', to: 'admin#dashboard', constraints: lambda { |req| req.subdomain == 'admin' } uses a lambda checking if request subdomain equals 'admin', which is precise and recommended.
    get '/dashboard', to: 'admin#dashboard', constraints: { domain: 'admin' } uses 'domain' key, which is incorrect for subdomain constraints.
  3. Final Answer:

    get '/dashboard', to: 'admin#dashboard', constraints: lambda { |req| req.subdomain == 'admin' } -> Option B
  4. Quick Check:

    Use lambda for precise subdomain check [OK]
Hint: Use lambda for exact subdomain constraints [OK]
Common Mistakes:
  • Using string instead of lambda or regex for subdomain
  • Confusing domain with subdomain key
  • Using regex that matches partial subdomains unintentionally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard