如何解决Rails模型命名空间冲突 [英] How to resolve Rails model namespace collision
问题描述
到目前为止的故事:
我有一个名为Term的模型的轨道应用程序。一切都很好,直到尝试安装黄瓜。运行
I have a rails app with a model named "Term". All is well until trying to install Cucumber. Upon running
rake cucumber
我获得
Term is not a class (TypeError)
这是因为Cucumber包含另一个gem,'term-ansicolor'(在控制台中执行漂亮的彩色文本输出)和term-ansicolor定义名为Term的模块。黄瓜在包括Rails模型之前包括term-ansicolor,因此,当加载term模型时,Term已经被定义为模块。顶级模块和类在Ruby中不能有相同的名称,因此会产生冲突。
This happens because Cucumber includes another gem, 'term-ansicolor' (to do the nifty colored text output in the console), and term-ansicolor defines a module named "Term". Cucumber includes term-ansicolor before including the Rails models, thus "Term" is already defined as a module when loading the "Term" model. Top-level modules and classes cannot have the same names in Ruby, thus the collision.
我不想重命名模型,我设置了修补term-ansicolor gem。这被证明比我想象的难。我将Term模块名称更改为ANSITerm,但我无法弄清楚如何获取Cucumber来加载我修改的gem,我已将其放入RAILS_ROOT / vendor / gems / term-ansicolor。
Preferring not to rename the model, I set about patching the term-ansicolor gem. This proved harder than I thought. I changed the Term module name to "ANSITerm", but I can't figure out how to get Cucumber to load my modified gem, which I've put into RAILS_ROOT/vendor/gems/term-ansicolor.
任何想法?
Any ideas? Am I barking up the wrong tree?
推荐答案
两种解决方案:
1)将应用的Term模型更改为其他类型。
1) Change your app's Term model to be something else.
2)补丁术语-ansicolor有一个命名空间Term,并使用该gem。
2) Patch term-ansicolor to have a namespaced Term and use that gem instead.
这篇关于如何解决Rails模型命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!