Is there a way to check if there exists an index action for a controller? Something like:
Controller.indexActionExists?
I have seen posts to check if specific routes exist, but those methods aren't working for me, since some of my index actions aren't associated with routes.
The action_methods
method is going to be the most expedient tool in this case, returning a Set
of method names. Give the following code a shot:
# Get a set of method names belonging to a controller called 'MyController'
methods = MyController.action_methods
if methods.include? "index"
puts "MyController has an index method"
else
puts "MyController lacks an index method"
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With