0
0
Ruby on Railsframework~10 mins

Route constraints in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a constraint that only allows numeric IDs in the route.

Ruby on Rails
get '/products/:id', to: 'products#show', constraints: { id: /[1]/ }
Drag options to blanks, or click blank then click option'
A[A-Z]+
B[a-z]+
C\w+
D\d+
Attempts:
3 left
💡 Hint
Common Mistakes
Using letters instead of digits in the regex.
Forgetting to escape backslashes.
2fill in blank
medium

Complete the code to restrict the route to only accept HTTP GET requests.

Ruby on Rails
match '/search', to: 'search#index', via: :[1]
Drag options to blanks, or click blank then click option'
Apost
Bdelete
Cget
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or other HTTP methods instead of GET.
3fill in blank
hard

Fix the error in the route constraint to only allow lowercase letters for the username parameter.

Ruby on Rails
get '/users/:username', to: 'users#show', constraints: { username: /[1]/ }
Drag options to blanks, or click blank then click option'
A[a-z]+
B[A-Z]+
C[a-zA-Z]+
D\d+
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase or digits in the regex.
Using a regex that allows uppercase letters.
4fill in blank
hard

Fill both blanks to create a route that only matches subdomains 'admin' or 'api'.

Ruby on Rails
constraints subdomain: /[1]|[2]/ do
  get '/dashboard', to: 'dashboard#index'
end
Drag options to blanks, or click blank then click option'
Aadmin
Buser
Capi
Dguest
Attempts:
3 left
💡 Hint
Common Mistakes
Including subdomains not intended to match.
Using commas instead of pipe in regex.
5fill in blank
hard

Fill all three blanks to define a route with constraints on HTTP method, subdomain, and numeric ID.

Ruby on Rails
constraints subdomain: /[1]/ do
  match '/items/:id', to: 'items#show', via: :[2], constraints: { id: /[3]/ }
end
Drag options to blanks, or click blank then click option'
Aapi
Bget
C\d+
Dadmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong subdomain or HTTP method.
Not using regex for numeric ID.