在Rails 5中,如何从已定义的模块继承?

问题描述 投票:0回答:1

我正在使用Ruby on Rails5。我已经在lib / audit / top_level_service.rb中创建了此基类

module Audit::Services
  class TopLevelService
    ...
  end
end

在我的应用程序的另一部分,app / services / accounting_service.rb中,我想从上面创建的这个模块继承,所以我尝试过]]

class PerkspotService < Audit::Services::TopLevelService
...
end

但这会导致“未初始化的常量Audit :: Services”错误。从正常的类定义中引用我的基类的正确方法是什么?

我正在使用Ruby on Rails5。我在lib / audit / top_level_service.rb模块Audit :: Services类TopLevelService中创建了这个基类... end在我的应用程序的另一部分,app / ...] >

inheritance module ruby-on-rails-5 base-class
1个回答
0
投票

原因是Autoloading and Reloading Constants。当您尝试访问PerkspotService时,它没有Audit::Services,并且没有文件audit/services.rb。您可以尝试多种方法来解决它。您可以像这样在application.rb中预定义名称空间:

Audit = Module.new
Audit::Services = Module.new
© www.soinside.com 2019 - 2024. All rights reserved.