本文介绍了ArgumentError:ApplicationController的副本已从模块树中删除,但仍处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ActiveAdmin(为Rails 4使用定制的gemset)与Rails 4.0.0.rc2一起使用。应用程序还具有基于railscasts 和。

I am using ActiveAdmin (with customized gemset for Rails 4) with Rails 4.0.0.rc2. Application also has custom-built authorization code based on railscasts #385 and #386.

当我更改ActiveAdmin资源文件中的某些内容并尝试刷新浏览器页面时,在 current_permission 方法:

When I change something in a ActiveAdmin resource file and try to refresh the browser page, I get this error at the current_permission method:

ApplicationController的副本已从模块树中删除,但仍处于活动状态!

如果再次尝试刷新,则会得到:

If I try a refresh again, I get:

我认为这个问题与更改源文件后在开发模式下自动加载类有关。我看到过类似的问题帖子,但它们适用于Rails2.3.x。另外,解决方案似乎在控制器中指定 unloadable 引发此错误,但我不确定在ActiveAdmin中将此代码段放在何处。

I think this problem has something to do with autoloading of classes in development mode, after a change in the source file. I have seen similar problem posts, but they are for rails 2.3.x. Also, the solution seems to be specifying unloadable in the controller throwing this error, but I am not sure where to put in this snippet in ActiveAdmin.

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

This might not have anything to do with ActiveAdmin either. It might be about how Permissions class has been built and its usage within Application Controller. If I add a skip_before_filter :authorize in the ActiveAdmin resource class, this error vanishes.

ApplicationController:

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

Permissions.rb:

Permissions.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

admin / courses.rb:

admin/courses.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(相关行):

Gemfile (relevant lines):

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:

ArgumentError:

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:循环依赖项:

RuntimeError: Circular Dependency:

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'

任何线索都会有所帮助。
让我知道您是否需要查看应用程序中的其他代码段。

Any clue will help.Let me know if you need to view other code snippets in the application.

推荐答案

我不确定为什么会这样,但是我找到了解决方法。更改以下内容:

I'm not sure exactly why this is happening, but I found a workaound. Change this:

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

为此:

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

在此出现:

# 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 命名空间中查找它。

The problem only occurs when you don't fully qualify the constant name, so Rails tries looking it up in the ApplicationController namespace.

这篇关于ArgumentError:ApplicationController的副本已从模块树中删除,但仍处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:00