Merge pull request #39886 from lanzhiheng/append-some-description-about-advance-constraints-in-block-form

[ci skip] Append some description about advance constraints
This commit is contained in:
Eileen M. Uchitelle 2020-08-17 08:45:16 -04:00 committed by GitHub
commit 6508a5f25d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -751,6 +751,35 @@ end
Both the `matches?` method and the lambda gets the `request` object as an argument.
#### Constraints in a block form
You can specify constraints in a block form. This is useful for when you need to apply the same rule to several routes. For example
```
class RestrictedListConstraint
# ...Same as the example above
end
Rails.application.routes.draw do
constraints(RestrictedListConstraint.new) do
get '*path', to: 'restricted_list#index',
get '*other-path', to: 'other_restricted_list#index',
end
end
```
You also use a `lambda`:
```
Rails.application.routes.draw do
constraints(lambda { |request| RestrictedList.retrieve_ips.include?(request.remote_ip) }) do
get '*path', to: 'restricted_list#index',
get '*other-path', to: 'other_restricted_list#index',
end
end
```
### Route Globbing and Wildcard Segments
Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example: