我有一个应用程序,当控制器类ActionDispatch::Routing::RouteSet::Dispatcher.controller_reference
不存在时,我在方法中动态创建控制器类,并借助Object.const_set
和Class.new
。
这工作正常,直到我修改这个动态创建的控制器所基于的控制器(这在开发中发生了很多,因为我不断地对控制器进行更改)。
这会引发以下异常:
ArgumentError
A copy of Base::FooController has been removed from the module tree but is still active!
我有一个Base::FooController
是父级的,我正在动态创建一个名为Bar::FooController
.
这个异常被抛出ActiveSupport::Dependencies.load_missing_constant
。有没有办法重新加载/重新创建这个类并避免异常?
activesupport/lib/active_support/dependencies.rb
def load_missing_constant(from_mod, const_name)
log_call from_mod, const_name
# I want to do a check here and recreate the controllers that are needed....
unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod)
raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
end
...