【问题标题】:Rails: "is not a module" errorRails:“不是模块”错误
【发布时间】: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


【解决方案1】:

更新

看来你 require 'integrations'new_mavenlink_project.rb 中的所有其他模块,虽然你不应该,但自动加载的重点是你不需要在 rails 应用程序的任何地方需要任何东西。要访问您的 NewProject 类,您只需调用:

Integrations::SetNetsuite::Scenarios::NewProject

在这种情况下,您不需要向autoload_paths 添加任何内容,rails 会从嵌套中生成它。

【讨论】:

    猜你喜欢
    • 2015-10-31
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多