【问题标题】:Rails modules and routingRails 模块和路由
【发布时间】:2012-04-18 12:07:58
【问题描述】:

我想在MyModule 中包含TestModule

# in test_module.rb
module TestModule
    SOMETHING = [1, 2, 3]
end

# in my_module.rb
module MyModule
    include TestModule

    def my_method
        "testing"
    end
end

我收到此错误:

Routing Error: uninitialized constant MyModule::TestModule

我已经仔细检查了 Rails 命名约定。 知道为什么这不起作用吗?

更多信息application.rb 中的 config.autoload_paths += ... 已被注释掉。但是/lib 中的其他模块正在以某种方式加载。

更多信息 2:我认为 rails 看不到新文件 test_module.rb。如果我将新模块添加到包含模块的现有文件中,那么包含新模块就可以了。 $LOAD_PATH 或其他东西是否有某种轨道清理或刷新过程?

【问题讨论】:

    标签: ruby-on-rails module routing include


    【解决方案1】:

    你也可以试试这个:

    # in my_module.rb
    
    load 'test_module.rb'
    
    module MyModule
       include ::TestModule
    
       def my_method
          "testing"
       end
    end
    

    引用顶级命名空间。

    【讨论】:

      【解决方案2】:

      尝试像这样在文件顶部添加“要求”:

      # in my_module.rb
      require 'test_module'
      module MyModule
        include TestModule
        ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-07
        • 1970-01-01
        • 1970-01-01
        • 2018-08-13
        • 1970-01-01
        • 2018-03-14
        • 1970-01-01
        • 2018-06-09
        相关资源
        最近更新 更多