Getting controller, action, and params for a given path

Hello,

I’m trying to find a programmatic way to get access to the routing table
such that I can discover what controller, action, etc., a given path
would map to. I could parse the path string myself, but I’d like to be
able to access controllers for non-standard routes, named routes, etc.

I’ve googled every combination of rails routing I could think of and
have yet to find anything useful. I also read through all the
ActionController::Routing docs.

If anyone knows a convenient way to do this or an existing helper /
plugin that provides this functionality, I’d appreciate the help.

Cheers,
Darrik Mazey
DMT Programming, LLC.

Darrik Mazey wrote:

If anyone knows a convenient way to do this or an existing helper /
plugin that provides this functionality, I’d appreciate the help.

Cheers,
Darrik Mazey
DMT Programming, LLC.

I offer this update because I’m still in need of assistance but also in
case anyone else has been looking for a similar solution. Hopefully
this update will be of assistance.

I’ve managed to find in the rails source the method
ActionController::Routing::Routes.recognize_path(path, env) which
returns a route hash of the form { :controller => ‘controller’, :action
=> ‘action’, :id => ‘id’ }.

So far so good. However, I’m having trouble with it “recognizing” the
id as the action and the action as the id for paths such as
‘/users/1/edit’. recognize_path() returns { :controller => ‘users’,
:action => ‘1’, :id => ‘edit’ } for this path. It seems as if
recognize_path() is ignoring any routes added with map.resources and
relying on the default route (/:controller/:action/:id) to recognize
paths. Is recognize_path() the underlying functionality that the rails
dispatcher itself uses to route requests, or am I off course?

In the meantime, I’m simply doing some simple heuristics on the returned
Route hash and reconstructing it. It’s an imperfect solution but
functional for now.

Cheers,
Darrik

Darrik Mazey wrote:

id as the action and the action as the id for paths such as
‘/users/1/edit’. recognize_path() returns { :controller => ‘users’,
:action => ‘1’, :id => ‘edit’ } for this path. It seems as if
recognize_path() is ignoring any routes added with map.resources and
relying on the default route (/:controller/:action/:id) to recognize
paths. Is recognize_path() the underlying functionality that the rails
dispatcher itself uses to route requests, or am I off course?

In the meantime, I’m simply doing some simple heuristics on the returned
Route hash and reconstructing it. It’s an imperfect solution but
functional for now.

Almost immediately after posting my last update, I went back through the
source for recognize_path() and realized I wasn’t passing in the
environment (which by default only consists of { :method =>
request.method }).

A call to
ActionController::Routing::Routes.recognize_path(’/users/1/edit’,
{:method => :get}) now properly yields { :controller => ‘users’, :action
=> ‘edit’, :id => ‘1’}. Hope someone else finds this useful.

Cheers,
Darrik