我正在尝试将模块添加到我的Rails 3.1应用程序中,之前我已经能够执行此操作,但现在使用我添加的最新模块无效。任何想法都非常感激
在application.rb
中# Custom directories with classes and modules you want to be autoloadable. config.autoload_paths += %W(#{Rails.root}/app/workers #{Rails.root}/lib/validators #{Rails.root}/lib/content_items #{Rails.root}/lib/booher_modules )
在lib / booher_modules / mongoid_counter_cache.rb
module Mongoid module CounterCache extend ActiveSupport::Concern module ClassMethods def counter_cache(options) ... some stuff ...
现在投票.rb:
class Vote include Mongoid::Document include Mongoid::Timestamps include Mongoid::CounterCache
无论何时我尝试启动应用程序,都会收到未初始化的常量错误:
Users/Tim/Sites/polco/app/models/vote.rb:4:in `': uninitialized constant Mongoid::CounterCache (NameError) from /Users/Tim/Sites/polco/app/models/vote.rb:1:in `' from /Users/Tim/.rvm/gems/ruby-1.9.2-p290@cba/bundler/gems/mongoid-ccae125ccfd8/lib/rails/mongoid.rb:66:in `load_model' ... so on
我试图在vote.rb中输入require'lib / mongoid_counter_cache.rb',但我得到了:
rails c /Users/Tim/.rvm/gems/ruby-1.9.2-p290@cba/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:306:in `rescue in depend_on': No such file to load -- lib/mongoid_counter_cache (LoadError)
答案 0 :(得分:2)
你遇到了这个问题,因为Rails试图包含“Mongoid :: CounterCache”。
为此,它正在自动加载路径中的某处寻找文件“mongoid / counter_cache.rb”。
因此...
...
lib/booher_modules/mongoid/counter_cache.rb
...
因此要修复......
mkdir -p lib/booher_modules/mongoid
mv lib/booher_modules/mongoid_counter_cache.rb lib/booher_modules/mongoid/counter_cache.rb
您的特定“require'lib / mongoid_counter_cache.rb'”不起作用的原因是因为它不会查看它在主要包含路径($ :)中查找的自动加载路径,其中不包含lib / booher_modules(仅配置自动加载)