1

我正在访问控制器 Security::MyController 中的模型 Company::Item。它给出错误未初始化常量 Security::Company::Item 。所以基本上它为给定的模型附加了'Security::'。其他一些模型并非如此,例如 Security::User(model in same module security)。对此有什么可能的解释?

4

1 回答 1

2

这是一个范围解析问题。你应该尝试使用::Company::IteminsideSecurity::MyController

根据 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::范围

于 2011-09-03T03:00:21.053 回答