【问题标题】:Adding lib to config autoload paths does not autoload my module in Rails 3将 lib 添加到配置自动加载路径不会在 Rails 3 中自动加载我的模块
【发布时间】:2023-03-09 19:41:01
【问题描述】:

将 lib 添加到配置自动加载路径不会在 Rails 3 中自动加载我的模块。

我添加到我的 config/application.rb 文件中。

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

在我的控制器中我添加了

require 'lib_util' (or)
include LibUtil         #both doesn't work

在我的 lib/lib_util.rb 文件中,我有以下模块

module LibUtil
  module ClassMethods
    def p_key(a,b)
      //mycode            
    end
  end
    def self.included(receiver)
       receiver.extend ClassMethods
    end
end

我收到错误未定义方法“p_key”。需要注意的重要一点是,我在模型中调用了相同的模块,它工作正常。但在我的控制器中,它不能识别模块。

有人可以指导我吗?

【问题讨论】:

  • 在您的控制器中尝试例如:class PostsController add include LibUtil::ClassMethods
  • @itsnikolay - 谢谢。它工作正常。

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2


【解决方案1】:

您是否尝试同时包含这两个模块?

include LibUtil::ClassMethods

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多