How to get list of controllers and actions in ruby on rails?

前端 未结 4 729
你的背包
你的背包 2020-12-07 20:50

In rails, I can get the name of the current controller via controller_name and the current action by calling action_name. I am looking for similar runtime reflection for fet

4条回答
  •  無奈伤痛
    2020-12-07 21:08

    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).

提交回复
热议问题