Reputation: 32933
Whenever i have a routing bug i always end up going through a cycle of "change config/routes.rb, reload page, watch log file to see which controller/action is called". It's quite slow, as the app in question is very large and loading the environment (which is done on each request in dev mode) takes a significant time.
Does anyone know a way to just query the router alone, eg call the router with a url and get a hash of controller, action & params back, without having to load my entire app every time?
This is for a rails 2.2.2 app btw. (Yes i know i should update, not an option atm unfortunately)
Upvotes: 0
Views: 43
Reputation: 32933
I figured out how to do this: the key was ActionController::Routing::Routes
which returns the built routes map, and the recognize_path
method called on this.
def test_route(url, method = :get)
method = method.to_s.downcase.to_sym
uri = Addressable::URI.parse(CGI.unescape(url))
path = uri.path
params = uri.query_values || {}
routes = ActionController::Routing::Routes;false
begin
routes_hash = routes.recognize_path(path, {:method => method})
HashWithIndifferentAccess.new(routes_hash.merge(params))
rescue ActionController::RoutingError
return nil
rescue Exception => e
raise $!
end
end
if the routes map can't resolve a route it will raise an ActionController::RoutingError
exception, and i catch this and return nil, to signify that no matching route was found. Any other exceptions just get bounced up.
Upvotes: 0
Reputation: 2536
Have a look at the Action Controller routing assertions:
actionpack/lib/action_controller/assertions/routing_assertions.rb
Upvotes: 2