我从/ plac.rb命名的Plac的Rails 3 - 不同类的行为在轨控制台VS轨服务器
模型在模型中定义的模型,这样中引用的自定义类:
class Model < ActiveRecord::Base
def notify_owner
notifier = BatchNotify.getInstance
end
end
BatchNotify在LIB /模块/ batch_notify.rb定义像这样:
class BatchNotify
def self.getInstance
env = Rails.env
if(env == "test")
return TestBatchNotify.new
else
BatchNotify.new
end
end
end
我加模块目录autoload_path:
config.autoload_paths += %W(#{config.root}/lib/modules)
奇怪的是,当notify_owner()在rails控制台上工作良好时。
然而,当我开始使用Rails服务器的Web服务器,并尝试通过在浏览器中使用的应用程序触发notify_owner,我得到以下错误:
未初始化的常量的Plac :: BatchNotify
首先,为什么在控制台和Web服务器中的行为不同?
二,为什么它仍然不能识别批量通知常量?
顺便说一句,我也试着在一个模块内定义BatchNotify并没有运气引用它作为模块:: BatchNotify ...
永远不要依赖于Rails的自动加载功能。始终要求你的依赖。在模型中添加**需要'modules/batch_notify'**。 **导轨控制台制作**应该有类似的问题。 – 2013-08-03 17:50:31
已经有一个很好的答案。但作为附加提示..如果您编写:: BatchNotify而不是BatchNotify,rails将搜索模型类作用域之外的常量。 – Mattherick
Mattherick我试过了。我得到这个错误:未初始化的常量BatchNotify –