How to list all developed routes programmatically

I use a program to authenticate my users in a Rails 4.1.9 application.

I need to list all the routes being developed in a javascript file.

I tried to do something like this:

all_routes = Rails.application.routes.routes.map do |route|
    route.path.spec.to_s.gsub('(.:format)', '')
end

It just prints all the rail routes. How can I only come up with?

I tried to check the controllers, but I do not know how to do this ...

+4
source share
2 answers

Use the route controller to determine if the route belongs to the development controller or not: route.defaults[:controller]it can become complicated if you configure or skip the default routes for your own:

, : "devise/sessions", , .starts_with? "devise" - . , !

+2

. .

# Ask devise what controllers is has mapped
devise_controllers = Devise.mappings.values.map do |mapping|
  mapping.controllers.map{|k, v| v}}
end.flatten.uniq
# Ask rails for the routes that resolve to devise controllers
Rails.application.routes.routes.select do |route|
  devise_controllers.include? route.defaults[:controller]
end
+1

All Articles