我正在考虑将我最喜欢的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