有没有办法检查 Controller 是否存在索引操作?就像是:
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
关于ruby-on-rails - Rails 检查 Controller 索引是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33817142/