Routing Strangeness in Rails 1.1.3
Jun 28th, 2006 by phil
Update: This problem was fixed in Rails 1.1.4. Kudos to the Rails core team for a quick response.
I just upgraded to Rails 1.1.3 and ran into some strangeness in the routing code. In my application I have a set of controllers that are dedicated to administrative functions so I have put them in a subdirectory of ‘app/controllers’ called ‘admin’. The controllers in this directory are all declared like:
class Admin::FooController < ApplicationController
# ...
end
This means that the URLs for these controllers all look like “http://myapp.com/admin/foo” giving a visual indication in the URL that this is part of the admin area.
Everything worked fine until I updated to the 1.1.3 release of Rails when I started getting 404 errors for any of the controllers in the admin directory. It turns out that the problem is in the method ActionController::RouteSet#recognize(). Actually, #recognize calls the #recognize_path method which returns nil. However, #recognize_path appears to be generated at runtime by some pretty hairy looking code.
I was able to find a couple of workarounds. Both seem to work and neither makes a lot of sense to me. The first is to require one of the admin controllers in routes.rb:
require 'application'
require 'admin/foo_controller'
Note that I only have to require one of the admin controllers and it doesn’t seem to matter which one. Also, I have to require the application controller as well since it isn’t loaded yet when routes.rb is loaded for the first time.
The second workaround is to create a route that includes one of the admin controllers:
map.connect '/admin/foo', :controller => 'admin/foo'
I suspect that this route is causing the Admin::FooController class to be required behind the scenes. Again, it doesn’t matter which controller I create a route to as long as it is in the admin directory.
I tried to debug the issue but ran into a brick wall with the code generation that is going on in routing.rb. If someone has an explanation for what is happening, please let me know. Otherwise, I hope this helps someone else work around the issue.






