aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Moss <maclover7@users.noreply.github.com>2016-01-24 09:46:16 -0500
committerJon Moss <maclover7@users.noreply.github.com>2016-01-24 09:46:16 -0500
commit3bebb65e86baac1792499c94679a4d4a0cc4acbd (patch)
treebc36e9bdf3bdf24f561b19244072244cdb11f957
parent8f35fcec568883f64df6cf0b04305d23bd49d615 (diff)
parent1307c8d0ea6e6c483076ee42670915bd62adf5f7 (diff)
downloadrails-3bebb65e86baac1792499c94679a4d4a0cc4acbd.tar.gz
rails-3bebb65e86baac1792499c94679a4d4a0cc4acbd.tar.bz2
rails-3bebb65e86baac1792499c94679a4d4a0cc4acbd.zip
Merge pull request #23213 from Ferdy89/docs_format_contraints_with_glob
Update the exception of format constraint in routes
-rw-r--r--guides/source/routing.md2
1 files changed, 2 insertions, 0 deletions
diff --git a/guides/source/routing.md b/guides/source/routing.md
index 09491f3c9a..5a745b10cd 100644
--- a/guides/source/routing.md
+++ b/guides/source/routing.md
@@ -706,6 +706,8 @@ end
NOTE: Request constraints work by calling a method on the [Request object](action_controller_overview.html#the-request-object) with the same name as the hash key and then compare the return value with the hash value. Therefore, constraint values should match the corresponding Request object method return type. For example: `constraints: { subdomain: 'api' }` will match an `api` subdomain as expected, however using a symbol `constraints: { subdomain: :api }` will not, because `request.subdomain` returns `'api'` as a String.
+NOTE: There is an exception for the `format` constraint: while it's a method on the Request object, it's also an implicit optional parameter on every path. Segment constraints take precedence and the `format` constraint is only applied as such when enforced through a hash. For example, `get 'foo', constraints: { format: 'json' }` will match `GET /foo` because the format is optional by default. However, you can [use a lambda](#advanced-constraints) like in `get 'foo', constraints: lambda { |req| req.format == :json }` and the route will only match explicit JSON requests.
+
### Advanced Constraints
If you have a more advanced constraint, you can provide an object that responds to `matches?` that Rails should use. Let's say you wanted to route all users on a blacklist to the `BlacklistController`. You could do: