rails/rails — Issue #4029: Routing fails if non-static constraint fails
Author: dvandersluis
Created: 2011-12-19 16:50:39+00:00
State: open

Consider the following:

class SubdomainConstraint
def matches?(request)
request.subdomain.present? and request.subdomain != "clients"
end
end

MyApp::Application.routes.draw do
match "/" => "other#jobs", :constraints => SubdomainConstraint.new
match "/" => "clients#index", :constraints => { :subdomain => "clients" }
end

With these routes, if I browse to `other.myapp.com`, I am correctly routed to `other#jobs`. However, if I browse to `clients.myapp.com`:

Routing Error
No route matches [GET] "/"

If I reverse the order of the routes, so that the route with a "static" constraint is first, both subdomains work as expected. However, if I have multiple "non-static" routes (using lambdas for simplicity):

MyApp::Application.routes.draw do
match "/" => "clients#index", :constraints => lambda{ |req| req.subdomain.present and req.subdomain == "clients" }
match "/" => "other#jobs", :constraints => lambda{ |req| req.subdomain.present and req.subdomain != "clients" }
end

In this case, only the first lambda seems to be evaluated, and the second route is never hit.

The same behaviour exists if the constraint is specified on a `scope` block or if the constraint uses a lambda instead of a class.