【问题标题】:ruby rails 4.1 module - inconsistent error - not initialized "uninitialized constant"ruby rails 4.1模块-不一致的错误-未初始化“未初始化的常量”
【发布时间】:2015-11-15 17:03:17
【问题描述】:

我在以下文件夹结构中使用 ruby​​ 2.1.5 和 rails 4.1:

- lib/
  - notifications/
    - notifications.rb
    - group_actions.rb


# notifications.rb
module Notifications

end


# group_actions.rb
class GroupActions
  self.do_something
end


# inside a controller
class Api::V1::PostsController < Api::BaseController
  include Notifications
  .
  .
  .
  def create
    Notifications::GroupActions.do_something
  end
  .
  .
  .
end

我还在“config/application.rb”中添加了这一行来自动加载我的模块

# config/application.rb
config.autoload_paths += %W(#{config.root}/lib/notifications)

这很好有时,有时会中断并引发错误“未初始化的常量通知::GroupActions”

这是不一致的,它在请求中起作用并在跟随时引发此错误!!! ...它可能会工作几天,休息几个小时,然后再次工作!!!

如果这个提示有帮助,我注意到它总是在重启服务器后的第一个请求上工作

请帮忙

【问题讨论】:

  • 如果你改用::Notifications::GroupActions.do_something会发生什么?

标签: ruby-on-rails ruby ruby-on-rails-4 uninitialized-constant ruby-2.1


【解决方案1】:

我可能是错的,但只是一个疯狂的猜测。 您包括通知模块(在此特定代码中 - 通知模块没有方法 group_actions)

我看到的是您使用 Notifications::GroupActions.do_something 的范围。


猜一猜: 内部控制器还包括 GroupActions


猜想2: 在 Notofications 模块中添加另一个模块

module Notifications
  module GroupActions
    def self.do_something

猜测3:

尝试继承。

module Notifications < GroupActions

module GroupActions < Notifications

希望这些解决方案中的任何一个都会有所帮助。

如果没有,对不起:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多