Reputation: 618
I am trying to write a script that can list all available routes and for each route, query the controller and action name (a bit like rake routes).
If I run Rails.application.routes.routes
I can see the data is all there. I just can't seem to access it.
@named_routes=
{"root"=>
#<ActionDispatch::Journey::Route:0x007fc2cb40a598
@app=
#<ActionDispatch::Routing::RouteSet::Dispatcher:0x007fc2cb40b448
@controller_class_names=#<ThreadSafe::Cache:0x007fc2cb40b420 @backend={}, @default_proc=nil>,
@defaults={:trailing_slash=>false, :controller=>"home", :action=>"show"},
@glob_param=nil>,
@constraints={:required_defaults=>[:trailing_slash, :controller, :action], :request_method=>/^GET$/},
@decorated_ast=nil,
@defaults={:trailing_slash=>false, :controller=>"home", :action=>"show"},
How do I access that hash tucked away in there with the controller name.
As a side, Is there a way to determine a path from this?
Upvotes: 3
Views: 4882
Reputation: 66837
This should work if I understand your question correctly:
Rails.application.routes.routes.named_routes.values.map(&:defaults)
Then you can do something like this:
Rails.application.routes.routes.named_routes.values.map do |route|
"#{route.defaults[:controller]}#{route.defaults[:action]}"
end
Upvotes: 7