You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Redmine 5(Rails 6.1.7.4)自定义插件适配Zeitwerk遇NameError求助

问题原因及解决方案

核心原因

Zeitwerk是约定优先的自动加载器,要求文件路径与常量的命名空间严格对应。你的文件路径是redmine_lutech_integration/lib/redmine_lutech_integration/catalogue_user_format.rb,Zeitwerk会期望该文件定义RedmineLutechIntegration::CatalogueUserFormat常量,但你实际定义的是Redmine::FieldFormat::CatalogueUserFormat,两者不匹配导致报错。

三种解决方法

方法1:调整常量结构匹配文件路径

修改代码中的命名空间,让常量与文件路径对应:

# redmine5/plugins/redmine_lutech_integration/lib/redmine_lutech_integration/catalogue_user_format.rb
module RedmineLutechIntegration
  class CatalogueUserFormat < Redmine::FieldFormat::RecordList
    # 原有逻辑保持不变
  end
end

Redmine::FieldFormat.add 'job', RedmineLutechIntegration::CatalogueUserFormat

这种方式完全符合Zeitwerk的约定,无需额外配置。

方法2:调整文件结构匹配常量结构

如果要保留Redmine::FieldFormat::CatalogueUserFormat的常量结构,需要调整文件路径为:

redmine5/plugins/redmine_lutech_integration/lib/redmine/field_format/catalogue_user_format.rb

同时修改代码中的继承(明确指定RecordList的完整命名空间):

module Redmine
  module FieldFormat
    class CatalogueUserFormat < Redmine::FieldFormat::RecordList
      # 原有逻辑保持不变
    end
  end
end

Redmine::FieldFormat.add 'job', Redmine::FieldFormat::CatalogueUserFormat

注意:这种方式可能与Redmine核心文件路径重叠,需确认不会引发加载冲突。

方法3:手动require跳过Zeitwerk自动加载

沿用你处理其他文件的方式,在插件的init.rb中手动require该文件:

# redmine5/plugins/redmine_lutech_integration/init.rb
require File.expand_path('../lib/redmine_lutech_integration/catalogue_user_format.rb', __FILE__)

这样Zeitwerk不会自动处理该文件,避免路径与常量不匹配的报错。

内容的提问来源于stack exchange,提问作者NinjaTurtles93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 08:40:17