有没有一种方法可以检查控制器是否存在索引操作?类似于下面这样的代码:
Controller.indexActionExists?
我看到过一些帖子来检查特定路由是否存在,但是这些方法对我不起作用,因为我的一些索引操作没有与路由相关联。
Controller.indexActionExists?
我看到过一些帖子来检查特定路由是否存在,但是这些方法对我不起作用,因为我的一些索引操作没有与路由相关联。
action_methods
方法是在这种情况下最有效的工具,它返回一个方法名称的 Set
。试一下以下代码:# 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