views:

113

answers:

4

Is there any way possible to get the child paths given a path?

I mean given the following routes:

map.resources :users do |user|
  user.resources :articles
  ...
end

If I give path = "users/1", is there any way to get the routes users/1/edit, users/1/articles, users/1/articles/new ?

Thanks in advance. -Satynos

A: 

I'm not sure exactly what you are asking but these are what I consider the go to resources for routing:

Rails Routing from the Outside In

Ruby on Rails routing demystified

srboisvert
A: 

Yep, the route definitions are very accessible. The following code should get you the list of routes for a specific controller:

rts = ActionController::Routing::Routes.routes.reject do |rt|
  rt.defaults[:controller] != "users" || !rt.significant_keys.index(:id)
end

You can run the following code in the console to see the routes:

rts = ActionController::Routing::Routes.routes.reject do |rt|
  rt.defaults[:controller] != "users" || !rt.significant_keys.index(:id)
end; nil
rts.each do |rt|
  puts "Route: #{rt.segments}"
end; nil

The segments are actually broken out into an array also, which means it should be easy to further filter the list to only routes that require a user id.

Jay Stramel
A: 

srboisvert: Thanks for those links, I will look into them.

Jay Stramel: I ran your code in the console, but that gives me all the routes where the controller is not user. I want to have the descendant routes given a route. I guess I need to further look into the routes implementation and try to interpolate those routes to get what I want. Thanks for the feedback.

-Satynos

satynos
Are you sure? It works in Rails v. 2.3.2.
Jay Stramel
+2  A: 

There is a rake task for doing that,

rake routes | grep "users"

From a nix machine outta do it

If there is something clever you want to do in a Rails app, might be worth checking the source for that under rails / railties

Omar Qureshi