【问题标题】:rails3.1 autoloading failurerails3.1 自动加载失败
【发布时间】:2011-11-24 16:40:12
【问题描述】:

我正在尝试将一个模块添加到我的 Rails 3.1 应用程序中,我之前已经能够做到这一点,但它现在不适用于我添加的最新模块。任何想法都非常感谢

在 application.rb 中

# 包含您希望自动加载的类和模块的自定义目录。 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 中

模块 Mongoid 模块计数器缓存 扩展 ActiveSupport::Concern 模块类方法 def counter_cache(选项) ……一些东西……

现在投票.rb:

班级投票 包括 Mongoid::Document 包括 Mongoid::Timestamps 包括 Mongoid::CounterCache

每当我尝试启动应用程序时,都会收到未初始化的常量错误:

Users/Tim/Sites/polco/app/models/vote.rb:4:in `': 未初始化常量 Mongoid::CounterCache (NameError) 来自 /Users/Tim/Sites/polco/app/models/vote.rb:1:in `' 来自 /Users/Tim/.rvm/gems/ruby-1.9.2-p290@cba/bundler/gems/mongoid-ccae125ccfd8/lib/rails/mongoid.rb:66:in `load_model' ... 很快

我尝试将 require 'lib/mongoid_counter_cache.rb' 放入 vote.rb 中,但我得到了:

导轨 c /Users/Tim/.rvm/gems/ruby-1.9.2-p290@cba/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:306:in `rescue independ_on':没有要加载的文件-- lib/mongoid_counter_cache (LoadError)

【问题讨论】:

  • 我现在也遇到同样的问题,你解决了吗?

标签: ruby ruby-on-rails-3.1


【解决方案1】:

您遇到此问题是因为 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

您的特定“需要'lib/mongoid_counter_cache.rb'”不起作用的原因是因为它不在自动加载路径中查找它在不包含lib/的主包含路径($ :)中查找booher_modules(只有自动加载配置)

【讨论】:

    猜你喜欢
    • 2011-08-11
    • 2023-03-18
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多