参数错误:ApplicationController的一个副本已从模块树中删除,但仍然处于活动状态。

51

我正在使用带有定制化Gemset的ActiveAdmin(用于Rails 4),与Rails 4.0.0.rc2一起使用。应用程序还基于railscasts #385#386构建了自定义授权代码。

当我更改ActiveAdmin资源文件中的内容并尝试刷新浏览器页面时,在current_permission方法处出现以下错误:

ArgumentError at /admin/courses

A copy of ApplicationController has been removed from the module tree but is still active!

如果我尝试再次刷新,会出现以下错误:

Circular dependency detected while autoloading constant Permissions

我认为这个问题与开发模式下类的自动加载有关,源文件更改后会出现此问题。我看过类似的问题贴子,但它们是针对rails 2.3.x的。另外,解决方案似乎是在引发此错误的控制器中指定unloadable,但我不确定在ActiveAdmin中放置此片段的位置。

这可能与ActiveAdmin无关。它可能与如何构建Permissions类及其在Application Controller中的使用有关。如果我在ActiveAdmin资源类中添加skip_before_filter :authorize,则此错误消失。

ApplicationController:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :authenticate_user!
  before_filter :authorize

  delegate :allow_action?, to: :current_permission
  helper_method :allow_action?

  delegate :allow_param?, to: :current_permission
  helper_method :allow_param?

  private
    def current_permission
      @current_permission ||= Permissions.permission_for(current_user)
    end

    def current_resource
      nil
    end

    def authorize
      if current_permission.allow_action?(params[:controller], params[:action], current_resource)
        current_permission.permit_params! params
      else
        redirect_to root_url, alert: "Not authorized."
      end
    end
end

权限.rb:

module Permissions
  def self.permission_for(user)
    if user.nil?
      GuestPermission.new
    elsif user.admin?
      AdminPermission.new(user)
    else
      UserPermission.new(user)
    end
  end
end

管理员/课程.rb:

ActiveAdmin.register Course do
  index do
    column :name
    column :description
    column :duration
    column :status
    column :price
    default_actions
  end

  filter :discipline
  filter :level
  filter :lessons
  filter :name
  filter :status
end
Gemfile(相关行):
gem 'rails', '4.0.0.rc2'

# Use puma as the app server
gem 'puma'

# Administration - Temporary github refs until rails 4 compatible releases
gem 'responders',          github: 'plataformatec/responders'
gem 'inherited_resources', github: 'josevalim/inherited_resources'
gem 'ransack',             github: 'ernie/ransack',            branch: 'rails-4'
gem 'activeadmin',         github: 'gregbell/active_admin', branch: 'rails4'
gem 'formtastic',          github: 'justinfrench/formtastic'

参数错误:

ArgumentError - A copy of ApplicationController has been removed from the module tree but is still active!:
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:445:in `load_missing_constant'
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:183:in `const_missing'
  rspec-core (2.13.1) lib/rspec/core/backward_compatibility.rb:24:in `const_missing'
  app/controllers/application_controller.rb:17:in `current_permission'
  app/controllers/application_controller.rb:25:in `authorize'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:417:in `_run__1040990970961152968__process_action__callbacks'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0.rc2) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (4.0.0.rc2) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.0.0.rc2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.0.0.rc2) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.0.0.rc2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.0.0.rc2) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.0.0.rc2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.0.0.rc2) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
  activerecord (4.0.0.rc2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.0.0.rc2) lib/abstract_controller/base.rb:136:in `process'
  actionpack (4.0.0.rc2) lib/abstract_controller/rendering.rb:44:in `process'
  actionpack (4.0.0.rc2) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.0.0.rc2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.0.0.rc2) lib/action_controller/metal.rb:231:in `block in action'
  actionpack (4.0.0.rc2) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  actionpack (4.0.0.rc2) lib/action_dispatch/routing/route_set.rb:48:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.0.rc2) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/routing/route_set.rb:655:in `call'
  omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
  omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
  omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
  omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/error_collector.rb:12:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/agent_hooks.rb:22:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/browser_monitoring.rb:16:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/developer_mode.rb:28:in `call'
  meta_request (0.2.7) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
  rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
  meta_request (0.2.7) lib/meta_request/middlewares/headers.rb:16:in `call'
  meta_request (0.2.7) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.1) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  activerecord (4.0.0.rc2) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.0.0.rc2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  activerecord (4.0.0.rc2) lib/active_record/migration.rb:369:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:373:in `_run__2183739952227501342__call__callbacks'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0.rc2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0.rc2) lib/rails/engine.rb:511:in `call'
  railties (4.0.0.rc2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  puma (2.1.1) lib/puma/server.rb:369:in `handle_request'
  puma (2.1.1) lib/puma/server.rb:246:in `process_client'
  puma (2.1.1) lib/puma/server.rb:145:in `block in run'
  puma (2.1.1) lib/puma/thread_pool.rb:92:in `block in spawn_thread'

运行时错误: 循环依赖:

RuntimeError - Circular dependency detected while autoloading constant Permissions:
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:460:in `load_missing_constant'
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:183:in `const_missing'
  rspec-core (2.13.1) lib/rspec/core/backward_compatibility.rb:24:in `const_missing'
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:686:in `remove_constant'
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:516:in `block in remove_unloadable_constants!'
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:516:in `remove_unloadable_constants!'
  activesupport (4.0.0.rc2) lib/active_support/dependencies.rb:300:in `clear'
  railties (4.0.0.rc2) lib/rails/application/finisher.rb:90:in `block (2 levels) in <module:Finisher>'
  activesupport (4.0.0.rc2) lib/active_support/file_update_checker.rb:75:in `execute'
  railties (4.0.0.rc2) lib/rails/application/finisher.rb:105:in `block (2 levels) in <module:Finisher>'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:377:in `_run__2753119820186226816__prepare__callbacks'
  activesupport (4.0.0.rc2) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/reloader.rb:62:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0.rc2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0.rc2) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0.rc2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0.rc2) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0.rc2) lib/rails/engine.rb:511:in `call'
  railties (4.0.0.rc2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  puma (2.1.1) lib/puma/server.rb:369:in `handle_request'
  puma (2.1.1) lib/puma/server.rb:246:in `process_client'
  puma (2.1.1) lib/puma/server.rb:145:in `block in run'
  puma (2.1.1) lib/puma/thread_pool.rb:92:in `block in spawn_thread'

任何线索都有帮助。 如果您需要查看应用程序中的其他代码片段,请告诉我。


请参阅https://github.com/spree/spree/issues/5104。 - Backo
6个回答

147
未来的访问者,虽然这个问题很古老并且已经有了一个已接受的答案,但我认为我应该对原始问题进行一些澄清。
当你试图从一个未被自动重新加载的类(在lib目录中)访问一个自动重新加载的类(在app目录中)时,将引发"ArgumentError: A copy of X has been removed from the module tree but is still active"错误。

2
很好。这是我在这个问题上找到的最好的解释,这个问题偶尔一直困扰着我。花了30秒钟来解决,现在我知道问题出在哪里了。非常感谢! - HakonB
是的,你说得对。你能在这里提供如何解决这个问题的建议吗? - Selvamani
4
这是为什么会发生的解释,但不是答案。这应该作为对原问题的评论添加。 - Donato
3
如果您在控制台中,请退出并重新打开控制台。这应该可以解决您的问题。 - Joseph Gill
4
感谢您解释了这个问题发生的原因。希望能听到一个普遍的解决方案。另外,像@Joseph Gill建议的那样退出/重新打开控制台在这里不是一个解决方案,因为这可能会在自然请求周期中发生。 Translated: 感谢解释此问题发生的原因。希望听到一个通用的解决方案。此外,因为这可能会发生在自然请求周期中,所以像 @Joseph Gill 建议的退出/重新打开控制台并不是一个解决方案。 - bigtunacan
显示剩余2条评论

54

我不确定为什么会出现这种情况,但我找到了一个解决方法。将此处进行更改:

def current_permission
  @current_permission ||= Permissions.permission_for(current_user)
end

转化为:

def current_permission
  @current_permission ||= ::Permissions.permission_for(current_user)
end

错误发生在ActiveSupport的这个位置:此处
# Load the constant named +const_name+ which is missing from +from_mod+. If
# it is not possible to load the constant into from_mod, try its parent
# module using +const_missing+.
def load_missing_constant(from_mod, const_name)
  log_call from_mod, const_name

  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
  # ...
end

只有在没有完全限定常量名称时,问题才会发生,因此Rails尝试在ApplicationController名称空间中查找。

1
谢谢Sean。我有一个晦涩的循环引用问题。::解决了这个问题。不再需要每次编辑文件时重新启动Rails。 - scarver2
我也遇到了一个非常简单的问题,即在控制器文件的外部引用类名,然后在控制器方法内部引用。在所有引用中添加完全限定名称(即::User)解决了这个问题! - Mitch VanDuyn
在我的ApplicationController中引用的类前加上::也解决了我的问题! - Rafael Sales

6
在你的development.rb文件中,尝试暂时添加以下内容:
config.cache_classes = true

对我来说有效。


这不是一个稳定的生产解决方案。 - Simon Franzen
2
是的,我同意。我没有明确提到这一点,但出于这个原因我写了“暂时”。 - vatsal
在99%的情况下,您都希望缓存类,因此说它不稳定是不正确的。然而,这有点hacky。 - Gerard
嘿@Ger,感谢回复。好的,总结一下。
  1. 在development.rb中暂时将config.cache_class = true并不能解决这个主题的问题(也许对于vatsal和他当前的gem-stack以及项目代码来说可以)。
  2. 当你遇到像这样的错误(我在生产系统中遇到过),这个答案仍然是不正确的,因为在production.rb中你应该始终缓存你的类。
- Simon Franzen
@SimonFranzen 可能我太过拘泥于细节了 :) 我同意你的观点,我的观点是,在生产环境中将 config.cache_classes = true 设置为完全安全/合理。 - Gerard
这不是我们在开发环境中想要的。 - Chenglu

3

我可以帮助您进行翻译。以下是涉及IT技术的内容:

我没有使用ActiveAdmin,但我刚刚遇到了一个与ApplicationController循环依赖有关的问题。

问题出在一个辅助模块声明不正确:

app/helpers/home_helper.rb

声明使用了以下代码:

module StaticHelper

修复后,一切都很顺利。我没有研究为什么会发生这种情况,Rails也没有返回更智能的错误信息。


2
您可以将此行放入config/initializers/active_admin.rb文件中:
在Rails <= 4中:
 config.skip_before_filter :load_footer_variables

在 Rails >= 5 中

config.skip_before_action :load_footer_variables

就像这里建议的那样。


1
实际上,在Rails 5中,这将是: skip_before_action - rept

-1
如果您在控制台中遇到此问题,只需关闭控制台而不是重新加载命令即可解决!这是因为自动重新加载的模块在控制台内运行时未被完全重新加载所导致的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接