我正在以与related question非常相似的方式使用config.autoload_paths
来从Rails 3项目中的lib目录加载类。
具体来说,我已将这些行添加到config / application.rb文件中:
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
但是,对于现有的类,此方法对我不起作用。当我添加像lib / extensions / string.rb这样的文件时:
class String
def foo
puts "foo"
end
end
我收到undefined method 'foo' for "":String
错误。通过各种搜索,我已经意识到这个问题与这些文件的延迟加载有关。我尝试使用config.eager_load_paths
,但无法让它工作。
答案 0 :(得分:1)
我正在完全你在我的应用程序中描述的内容,唯一的区别是我还有一个名为 extensions.rb的初始化程序使用以下代码:
Dir.glob('lib/extensions/*').each { |f| require f }