This project is archived and is in readonly mode.
defining subdomain constraints in router with various TLD lengths
Reported by Jakub Suder | August 23rd, 2010 @ 11:33 AM | in 3.0.2
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.
Comments and changes to this ticket
-
Andrew White August 23rd, 2010 @ 12:14 PM
- 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
-
Andrew White August 23rd, 2010 @ 12:20 PM
Another workaround would be to use a regexp and host:
constraints :host => /^api\.(localhost|rubyonrails\.org\.uk)$/ do ... end
-
Andrew White August 23rd, 2010 @ 12:43 PM
- State changed from open to wontfix
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>