[英]How do I call my namespaced class from the lib directory
I have a class that's 3 levels deep in my lib directory that I'm trying to call but I get an uninitialized constant error. 我试图调用的lib目录中有一个3级深的类,但出现未初始化的常量错误。 My class and directory structure looks like this: 我的类和目录结构如下所示:
file name: lib/my_module/my_second_module/my_third_module/my_class.rb 文件名:lib / my_module / my_second_module / my_third_module / my_class.rb
module my_module
module my_second_module
module my_third_module
class my_class
def self.something
do something...
end
end
end
end
end
I'm trying to call this class using the rails console but it returns 'uninitialized constant my_module'. 我正在尝试使用rails控制台调用此类,但它返回“未初始化的常量my_module”。 I run this command and get this error: 我运行此命令并得到以下错误:
MyModule::MySecondModule::MyThirdModule::MyClass.something
Also I've include the following in config/application.rb 我也将以下内容包含在config / application.rb中
config.autoload_paths += Dir["#{config.root}/lib/my_module/**/"]
尝试::MyModule::MySecondModule::MyThirdModule::MyClass.something
I fixed my problem by using another method to load my modules in the lib folder. 我通过使用另一种方法将模块加载到lib文件夹中解决了我的问题。 For some reason, the way I had did not load my lib files: 由于某种原因,我没有加载我的lib文件的方式:
config.to_prepare do
Dir.glob(File.join(File.dirname(__FILE__), "../lib/my_module/**/*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.