ruby-on-rails - Rails 检查 Controller 索引是否存在

标签 ruby-on-rails

有没有办法检查 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/

相关文章:

ruby-on-rails - 我正在尝试通过在 Rails 中使用 webpacker 来安装 toastr

ruby-on-rails - Sidekiq 不发送电子邮件

ruby-on-rails - 在 Ruby 中插入用户输入字符串

ruby-on-rails - 更新时的 attr_accessor 和密码验证

ruby-on-rails - 如何将变量 link_to 定义为外部 URL

ruby-on-rails - 无方法错误 : undefined method `each_value' when parsing YAML array

ruby-on-rails - 将Rails ActiveRecord转换为原始SQL

ruby-on-rails - 将字符串转换为数组

ruby-on-rails - 捕获所有异常并显示动态错误页面 Ruby on Rails 4

ruby-on-rails - Mongoid3/助力车 : database=admin command={:ismaster=>1}