From 1307c8d0ea6e6c483076ee42670915bd62adf5f7 Mon Sep 17 00:00:00 2001 From: Fernando Seror Date: Sat, 23 Jan 2016 13:39:38 -0600 Subject: Update the exception of format constraint in routes Per https://github.com/rails/rails/issues/20264 [ci skip] --- guides/source/routing.md | 2 ++ 1 file changed, 2 insertions(+) (limited to 'guides/source/routing.md') 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: -- cgit v1.2.3