How to get list of controllers and actions in ruby on rails?
The easiest way to get a list of controller classes is:
ApplicationController.descendants
However, as classes are loaded lazily, you will need to eager load all your classes before you do this. Keep in mind that this method will take time, and it will slow down your boot:
Rails.application.eager_load!
To get all the actions in a controller, use action_methods
PostsController.action_methods
This will return a Set
containing a list of all of the methods in your controller that are "actions" (using the same logic Rails uses to decide whether a method is a valid action to route to).
PostsController.action_methods
will return all actions of PostsController
including inherited, it's not what I want, I found PostsController.instance_methods(false)
, which will return all instance methods of PostsController
and not include inherited, exactly what I want.