我正在考虑将我最喜欢的CMS之一更新到Rails 7,该CMS已在github(PushType)上存档.只是自从Rails 6以来我就没有编码过Rails.显然,Rails 7中有关自动加载方法的一些内容发生了变化. 我得到了这个错误:

NameError: uninitialized constant PushType::ApplicationControllerMethods
          include PushType::ApplicationControllerMethods
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

对于发动机中的这条线路:

      initializer 'push_type.application_controller' do
        ActiveSupport.on_load :action_controller do
          # ActionController::Base.send :include, PushType::ApplicationControllerMethods
          include PushType::ApplicationControllerMethods
        end
      end
  • 引擎位于root/core/lib/push_typ/core/engine.rb
  • 所讨论的控制器方法的定义位置位于:root/core/app/controlers/concessions--在concessions内目录命名空间应该起作用,但事实并非如此,在控制器关注目录中,方法位于push_types/application_control_Methods.rb

鉴于我的语言中断,我不知道自己在做什么.但在try 解决这个问题时,我try 了自动加载与gem引擎内的目录有关的内容,如下所示:

    config.autoload_paths << File.expand_path("../../../app/controllers/concerns", __FILE__) <<
                             # File.expand_path("../../../app/controllers/concerns/push_type", __FILE__)
                             File.expand_path("../../../app/helpers", __FILE__)

完整的engine.rb文件如下:

module PushType
  module Core
    class Engine < ::Rails::Engine
      isolate_namespace PushType
      engine_name 'push_type'

      config.autoload_paths << File.expand_path("../../../app/controllers/concerns", __FILE__) <<
                             # File.expand_path("../../../app/controllers/concerns/push_type", __FILE__)
                             File.expand_path("../../../app/helpers", __FILE__)

      # config.autoload_paths << "#{root}/app/controllers/concerns" <<
      #                         "#{root}/app/controllers/concerns/push_type" <<
      #                         "#{root}/app/helpers"

      # lib = root.join("lib")
      # config.autoload_once_paths.ignore(
      #   lib.join("assets"),
      #   lib.join("tasks"),
      #   lib.join("generators")
      # )

      config.generators do |g|
        g.assets false
        g.helper false
        g.test_framework  :test_unit, fixture: false
        g.hidden_namespaces << 'push_type:dummy' << 'push_type:field'
      end

      config.assets.precompile += %w(
        *.gif *.jpg *.png *.svg *.eot *.ttf *.woff *.woff2
      )

      config.to_prepare do
        Rails.application.eager_load! unless Rails.application.config.cache_classes
      end

      initializer 'push_type.dragonfly_config' do
        PushType.config.dragonfly_secret ||= Rails.application.secrets.secret_key_base
        PushType.dragonfly_app_setup!
      end

      initializer 'push_type.application_controller' do
        ActiveSupport.on_load :action_controller do
          # ActionController::Base.send :include, PushType::ApplicationControllerMethods
          include PushType::ApplicationControllerMethods
        end
      end

      initializer 'push_type.menu_helpers' do
        ActiveSupport.on_load :action_view do
          include PushType::MenuBuilder::Helpers
        end
      end
    end
  end
end

推荐答案

现在还为时过早,Rails 7中尚未准备好重新加载代码.

然而,碰巧的是,这是一个trap .这就是为什么Rails 7不允许那么早地访问重新命名类.由于该模块包含在非重命名类(AC::Base)中,因此重命名它是没有意义的,因为重新加载对该包含的模块没有影响.

请删除自定义autoload_paths配置,并将引擎的关注点和助手目录添加到autoload_once_paths中.该集合中的不可重新命名的类和模块较早可用.

Ruby-on-rails相关问答推荐

在Ruby on rails中,重复访问ActiveRecordModel返回相同的对象:预期行为还是错误?

引用连接 Rails 表中枚举的查询

如何使用继承类 (STI) 获取 Ruby on Rails 类中基类的实例

为什么 Image_tag 产生images/...而不是assets/....?

Rails 3 的data-method='delete'如何优雅地降级?

rails 3,简单数组的Kaminari分页

使用回形针进行简单裁剪

更改 form_for rails 3.1 生成的 html 表单 ID

什么是 Ruby 中的 Python 文档字符串?

Rails 4网站图标问题

带有查询字符串参数的 Rails 动作缓存

Rails,获取模型中的资源路径

使用 AJAX 向 Rails 发送 Authenticity Token 的正确方法

参数错误:范围主体需要可调用

rails 单数资源还是复数?

在 rails 3 中批量插入

您推荐哪些工具来分析 Rails 应用程序?

Rails 3 返回 HTTP 406 Not Acceptable?

在 Rails 协会中未找到名为的协会可能拼写错误的问题

在 GIT 中处理 Rails db/schema.rb 文件的正确方法是什么?