如何解决 Rails 模型命名空间冲突 [英] How to resolve Rails model namespace collision

查看:33
本文介绍了如何解决 Rails 模型命名空间冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止的故事:

我有一个名为Term"的模型的 rails 应用程序.一切都很好,直到尝试安装 Cucumber.运行时

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"的模块.Cucumber 在包含 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.

不想重命名模型,我着手修补术语 ansicolor gem.事实证明这比我想象的要难.我将 Term 模块名称更改为ANSITerm",但我不知道如何让 Cucumber 加载我已将其放入 RAILS_ROOT/vendor/gems/term-ansicolor 的修改后的 gem.

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) 修补 term-ansicolor 以具有命名空间的 Term 并改用该 gem.

2) Patch term-ansicolor to have a namespaced Term and use that gem instead.

这篇关于如何解决 Rails 模型命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆