Ruby on rails 4 Rails 4:生产环境中的超类不匹配异常,可以';不可能
我目前正在开发一个更大的rails应用程序,它将引擎用于子应用程序,具有以下文件夹结构:Ruby on rails 4 Rails 4:生产环境中的超类不匹配异常,可以';不可能,ruby-on-rails-4,exception,superclass,rails-engines,subclassing,Ruby On Rails 4,Exception,Superclass,Rails Engines,Subclassing,我目前正在开发一个更大的rails应用程序,它将引擎用于子应用程序,具有以下文件夹结构: - app \- controllers \- global \- base_controller.rb \- configurations_controller.rb \- application_controller.rb - ... - engines \- engine_1 \- app \- controller
- app
\- controllers
\- global
\- base_controller.rb
\- configurations_controller.rb
\- application_controller.rb
- ...
- engines
\- engine_1
\- app
\- controllers
\- engine_1
\- application_controller.rb
\- global
\- base_controller.rb
\- configurations_controller.rb
- ...
控制器的设置如下所示:
# app/controllers/global/base_controller.rb
class Global::BaseController < ApplicationController
end
# app/controllers/global/configurations_controller.rb
class Global::BaseController < Global::BaseController
end
# engines/engine_1/app/controllers/engine_1/application_controller.rb
module Engine1
class ApplicationController < ::ApplicationController
end
end
# engines/engine_1/app/controllers/engine_1/global/base_controller.rb
require_dependency "engine_1/application_controller"
module Engine1
class Global::BaseController < ApplicationController
end
end
# engines/engine_1/app/controllers/engine_1/global/configurations_controller.rb
module Engine1
class Global::ConfigurationsController < Global::BaseController
end
end
在开发环境中,一切都按预期工作。但在生产环境中,当应用程序启动并且类正在急切地加载时,当它尝试加载模块Engine1
中的Global::BaseController
时,我会得到一个超类不匹配的异常
我(暂时)克服了重命名Engine1::Global::BaseController时出现的问题,但我不明白为什么这不能仅在生产模式下工作?在Rails中生产时,控制器的名称空间要严格得多。Rails需要直接文件夹路径。如果您不希望这样做,则应使用范围界定,例如:
scope :path => "account" do
resources :users
end
在Rails中进行生产时,控制器的名称空间要严格得多。Rails需要直接文件夹路径。如果您不希望这样做,则应使用范围界定,例如:
scope :path => "account" do
resources :users
end
当重新定义一个类时,超类不匹配
错误就会发生。根据您的问题,错误在BaseController上。下面是我的理论,看看你发布的内容。BaseController被多次定义,因此加载此代码时BaseController不明确:
module Engine1
class Global::ConfigurationsController < Global::BaseController
end
end
模块引擎1
类Global::ConfigurationsController
在加载时,BaseController似乎存在两种定义
# app/controllers/global/base_controller.rb
class Global::BaseController < ApplicationController
end
# app/controllers/global/configurations_controller.rb
class Global::BaseController < Global::BaseController
end
#app/controllers/global/base#u controller.rb
类全局::BaseController
那么应该加载哪个BaseController呢?对您来说,这里应该做什么是显而易见的,我想我理解您试图通过向配置添加间接级别来做什么。但是Rails在加载时将这两个定义视为相同的名称,并抛出超类错误
这是我对你的问题的看法。我很好奇你怎么想 重新定义类时会发生超类不匹配
错误。根据您的问题,错误在BaseController上。下面是我的理论,看看你发布的内容。BaseController被多次定义,因此加载此代码时BaseController不明确:
module Engine1
class Global::ConfigurationsController < Global::BaseController
end
end
模块引擎1
类Global::ConfigurationsController
在加载时,BaseController似乎存在两种定义
# app/controllers/global/base_controller.rb
class Global::BaseController < ApplicationController
end
# app/controllers/global/configurations_controller.rb
class Global::BaseController < Global::BaseController
end
#app/controllers/global/base#u controller.rb
类全局::BaseController
那么应该加载哪个BaseController呢?对您来说,这里应该做什么是显而易见的,我想我理解您试图通过向配置添加间接级别来做什么。但是Rails在加载时将这两个定义视为相同的名称,并抛出超类错误
这是我对你的问题的看法。我很好奇你怎么想 对不起,我还是不明白。控制器在发动机中。引擎是完全可装载的,因此在隔离的命名空间中。。。在routes.rb
中,它声明mount Engine1::Engine,at:'/Engine_1',as::Engine_1
你能详细说明你的答案吗?你能为你的routes文件添加一个要点吗?添加到我原来的问题中。对不起,我还是不明白。控制器在发动机中。引擎是完全可装载的,因此在隔离的命名空间中。。。在routes.rb
中,它声明mount Engine1::Engine,at:'/Engine_1',as::Engine_1
你能详细说明你的答案吗?你能为你的routes文件添加要点吗?添加到我原来的问题中。你是对的。我必须为父类提供全范围的类名,因为它似乎没有映射到封装模块。学到了一些新东西,谢谢!不客气!很高兴这有帮助。有时候,对一个问题需要不同的眼光。你是对的。我必须为父类提供全范围的类名,因为它似乎没有映射到封装模块。学到了一些新东西,谢谢!不客气!很高兴这有帮助。有时候,对一个问题需要不同的眼光。