Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

defining subdomain constraints in router with various TLD lengths

#5430

In the new router, it's possible to define a constraint on the subdomain field, like this:

constraints(:subdomain => 'api') do ... end

However, the subdomain method in request takes an argument that says how long the TLD domain is. Default is 1, so if you have a site like server.com, it works correctly, but for other sites (server.co.uk, localhost) you need to call request.subdomain(2) or request.subdomain(0) respectively.

The problem is that while it's possible to do that in a controller method, it's not possible in the router, which makes the constraints(:subdomain) useless in most cases (unless you use a custom class for constraints). This could be solved by changing the request.subdomain() method so that it takes the TLD length from some kind of global setting which can be set in somewhere in config/ directory.

Reported by Jakub Suder · August 23rd, 2010 @ 11:33 AM

State: wontfix
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. Andrew White
    Andrew White
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to Andrew White
    • Importance changed from to Low

    Not sure about adding another global config option but as a workaround you can do the following:

      constraints{ |req| req.subdomain(req.host == 'localhost' ? 0 : 2) == 'api' } do
        ...
      end
    

    August 23rd, 2010 @ 12:14 PM

  2. Andrew White
    Andrew White

    Another workaround would be to use a regexp and host:

      constraints :host => /^api\.(localhost|rubyonrails\.org\.uk)$/ do
        ...
      end
    

    August 23rd, 2010 @ 12:20 PM

  3. Andrew White
    Andrew White
    • State changed from open to wontfix

    August 23rd, 2010 @ 12:43 PM

  4. Jeremy Kemper