我有 2 个可安装的引擎,分别名为 Authorization 和 People。 People 有一个名为 User 的模型,Authorization 有一个嵌套模块 Authorization::People::User,我只使用它的模块功能。
因此,如果 Rails 混淆了 Authorization 的 People::User 与 People 引擎中的 User 模型,就会出现问题。
当我启动服务器并访问路径时,它提示没有构造函数,当我使用 RSpec 时进一步说明了这一点,它提示缺少新方法。当我将授权内的 user.rb 重命名为不冲突的名称时,问题就消失了。我认为这不应该发生的原因是因为外部模块 Authorization 应该防止任何名称冲突。
在授权引擎的 engine.rb 文件中,我将 lib 目录放入加载路径中,如下所示
lib -> authorization -> engine.rb
module Authorization
class Engine < ::Rails::Engine
isolate_namespace Authorization
config.autoload_paths += Dir["#{config.root}/lib/**/"]
end
end
这是我创建的用于处理授权的模块 lib -> authorization -> people -> user.rb
module Authorization
module People
module User
#If any of these methods returns true there is an error
def self.auth_user(editingUser,tokenUser)
if editingUser != tokenUser
#Can only edit your own data
return true
else
return false
end
end
end
end
end
这就是我在用户引擎中调用授权的方式
::Authorization::People::User.auth_user(@user,@instance_hash["current_user"])
错误可能与加载路径中的优先级有关,或者 我可能错误地定义了我的模块,但我一直在绞尽脑汁尝试 弄清楚这一点。任何帮助将不胜感激!!
最佳答案
因为我只在模块中使用授权引擎,所以我决定 制作一个简单的 Rails 插件/gem。我仍然想要一个更好的解决名称冲突的方法 问题所以我不会关闭问题
关于ruby-on-rails - Rails 4 混淆了我的一个引擎模型与另一个引擎库模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25123182/