Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the list of actions available for a controller?

I did MyController.methods and it listed EVERYTHING... things I didn't even know controllers could do!

How do I return the list of actions, such as create, edit, new, destroy, other_action, other_non_protected_or_private_method?

Using Ruby on Rails 2.3.8

like image 847
NullVoxPopuli Avatar asked Mar 07 '12 20:03

NullVoxPopuli


People also ask

What is action controller?

Action Controller is the C in MVC. After the router has determined which controller to use for a request, the controller is responsible for making sense of the request and producing the appropriate output.

How do I add actions to my controller?

Adding an Action to a Controller You add a new action to a controller by adding a new method to the controller. For example, the controller in Listing 1 contains an action named Index() and an action named SayHello(). Both methods are exposed as actions.


2 Answers

Not sure if this will work in 2.38 but I figured it was worth a shot:

To quote the relevant part:

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

like image 159
ScottJShea Avatar answered Nov 06 '22 13:11

ScottJShea


Use #instance_methods(false) to retrieve only controller's specific actions:

CustomController.instance_methods(false)
=> ["index", "update", "show", "custom_action", "another_action"]

Hope this helps!

like image 26
LuisVM Avatar answered Nov 06 '22 13:11

LuisVM