【发布时间】:2015-07-30 15:23:26
【问题描述】:
我正在使用命名空间和文件夹在 Rails 中对我的模型进行分组,并且遇到了一个问题,即文件未加载并引发 TypeError - is not a module 错误。
这是跟踪的顶部:
17:06:26 web.1 | TypeError - SetNetsuite is not a module:
17:06:26 web.1 | app/models/integrations/set_netsuite/scenarios/new_mavenlink_project.rb:2:in `<module:Integrations>'
17:06:26 web.1 | app/models/integrations/set_netsuite/scenarios/new_mavenlink_project.rb:1:in `<top (required)>'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/dependencies.rb:443:in `block in load_file'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/dependencies.rb:633:in `new_constants_in'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/dependencies.rb:442:in `load_file'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/dependencies.rb:342:in `require_or_load'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/dependencies.rb:480:in `load_missing_constant'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/dependencies.rb:180:in `const_missing'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/inflector/methods.rb:240:in `block in constantize'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/inflector/methods.rb:236:in `constantize'
17:06:26 web.1 | activesupport (4.1.10) lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
我的代码如下所示:
module Integrations
module SetNetsuite
module Scenarios
class NewProject < Scenario
end
end
end
end
我的文件夹结构是:
- models/
- integrations/
- set_netsuite/
- scenarios/
- new_project.rb
我正在自动加载/models 中的所有内容:
config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
如果我将模型更改为此,它可以工作:
module Integrations
class SetNetsuite::Scenarios::NewProject < Scenario
end
end
我知道命名空间类的行为与嵌套模块略有不同,但我更愿意使用嵌套模块方法。
任何帮助或建议将不胜感激。提前致谢!
【问题讨论】:
-
集成模块缺少
end -
非常感谢。不幸的是,这只是我的问题记录中的错误类型。代码中存在“缺失”端。
-
您是否已经在其他地方定义了
SetNetsuite类? -
不,我没有在别处定义过该类
标签: ruby-on-rails ruby ruby-on-rails-4 model namespaces