Ruby on rails:模型名称未初始化错误(模块不匹配)



我正在访问控制器Security::MyController中的模型Company::Item。它给出未初始化的常量Security::Company::Item错误。基本上它是为给定模型追加'Security::'对于其他一些模型,例如Security::User(相同模块安全性中的模型),情况并非如此。这有什么可能的解释呢?

这是一个作用域解析问题。您应该尝试在Security::MyController

中使用::Company::Item

根据Ruby语言规范

::Something is a shortcut for Object::Something. The idea is that ::Something 
should look up a constant called Something in the global scope, but since ruby 
doesn't truly have a global scope, it looks it up in the Object class, which is 
the nearest thing in ruby to a global scope.

前缀::将阻止Ruby在该上下文中应用默认作用域,在您的情况下,是Security::作用域

最新更新