在可安装的rails引擎中运行ActiveAdmin时出错:无法加载此类文件 - web_app / admin

时间:2015-07-06 06:04:00

标签: ruby-on-rails-4 activeadmin

我正在将rails应用程序从单个应用程序转换为多个引擎。

我正在尝试将ActiveAdmin移动到运行前端应用程序的引擎中。

堆栈是:

  • Ruby 4.2.2
  • Rails 4.1
  • Devise 3.5.1
  • 来自github master分支的ActiveAdmin。

我在gemspec中包含了ActiveAdmin:

s.add_dependency 'activeadmin'

在我的gemfile中:

gem 'activeadmin', github: 'activeadmin'

在我的web_app / lib文件夹中,web_app.rb文件如下所示:

require "web_app/engine"

module WebApp
  extend ActiveSupport::Autoload
  autoload :Admin
end

web_app / lib / web_app中的引擎文件包含以下内容:

require 'devise'
require 'activeadmin'

module WebApp
  class Engine < ::Rails::Engine
    isolate_namespace WebApp

    initializer "web_app.assets.precompile" do |app|
      app.config.assets.precompile += %w( web_app/common.css web_app/desktop.css eb_app/common_tablet_mobile.css web_app/mobile.css web_app/tablet.css ie/json2.js )
    end

    initializer :web_app do
      ActiveAdmin.application.load_paths += Dir[File.dirname(__FILE__) + '/admin']
    end
  end
end

routes文件如下所示:

WebApp::Engine.routes.draw do

  ActiveAdmin.routes(self)
  ...
end

所有ActiveAdmin资源都存储在web_app / lib / web_app / admin。

以管理员用户身份登录后,将引发以下错误:

cannot load such file -- web_app/admin

activesupport (4.1.11) lib/active_support/inflector/methods.rb:240:in `block in constantize'
activesupport (4.1.11) lib/active_support/inflector/methods.rb:236:in `constantize'
activesupport (4.1.11) lib/active_support/dependencies.rb:552:in `get'
activesupport (4.1.11) lib/active_support/dependencies.rb:583:in `constantize'
actionpack (4.1.11) lib/action_dispatch/routing/route_set.rb:78:in `controller_reference'
actionpack (4.1.11) lib/action_dispatch/routing/route_set.rb:68:in `controller'
actionpack (4.1.11) lib/action_dispatch/routing/route_set.rb:46:in `call'
actionpack (4.1.11) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.11) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.11) lib/action_dispatch/routing/route_set.rb:692:in `call'
railties (4.1.11) lib/rails/engine.rb:514:in `call'
railties (4.1.11) lib/rails/railtie.rb:194:in `method_missing'
actionpack (4.1.11) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.11) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.11) lib/action_dispatch/routing/route_set.rb:692:in `call'
bullet (4.14.7) lib/bullet/rack.rb:10:in `call'
rack-mobile-detect (0.4.0) lib/rack/mobile-detect.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/cookies.rb:562:in `call'
activerecord (4.1.11) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.11) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.11) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.11) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.11) lib/rails/rack/logger.rb:22:in `call'
request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.11) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
actionpack (4.1.11) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
railties (4.1.11) lib/rails/engine.rb:514:in `call'
railties (4.1.11) lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
/Users/kfranzen/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/kfranzen/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/kfranzen/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

引擎的目录列表

wep_app/
  app/
    assets/
    controllers/
    helpers/
    mailers/
    models/
    views/
  config/
    initializers/
      active_admin.rb
  lib/
    web_app.rb
    web_app/
      engine.rb
      version.rb
      admin/
        resource files
    tasks/
  spec/

更新问题的缩小版本:

我缩小了在挂载引擎中加载ActiveAdmin的问题。

首先,我将上面记录的引擎更改为完整引擎,以便所有路由都由父应用程序继承。这解决了访问ActiveAdmin路由的主要问题。

因此,我已将ActiveAdmin和其他管理员特定代码移至单独的引擎&#39; admin&#39;发动机。

admin.gemspec文件包括:

s.add_dependency 'activeadmin'

Gemfile包含以下行以提取当前版本:

gem 'activeadmin', github: 'activeadmin'

admin / lib文件夹包含admin.rb文件,如下所示:

require "admin/engine"

module Admin
  extend ActiveSupport::Autoload
end

admin / lib / admin中的引擎文件包含以下内容:

require 'activeadmin'
require 'activeadmin_hstore_editor'
require 'active_admin_csv_import'
require 'ckeditor'

module Admin
  class Engine < ::Rails::Engine

    initializer "admin.assets.precompile" do |app|
      app.config.assets.precompile += %w( vendor.css vendor.js ckeditor/* )
    end

    initializer :admin do
      ActiveAdmin.application.load_paths += Dir[File.dirname(__FILE__) + '/admin']
    end
  end
end

routes文件如下所示:

Admin::Engine.routes.draw do

  ActiveAdmin.routes(self)

end

所有ActiveAdmin资源都存储在web_app / lib / admin / admin。

只要引擎加载时,ActiveAdmin就能正常工作:

isolate_namespace Admin

如果添加了isolate_namespace,则会出现上一节中报告的错误。

将引擎作为可安装引擎加载时,父Rails.application.routes包含以下内容:

mount Admin::Engine, at: '/', as: 'admin'

2 个答案:

答案 0 :(得分:0)

你的问题在这里:

module WebApp
  extend ActiveSupport::Autoload
  autoload :Admin
end

Admin是什么?

  • 如果您的意思是ActiveAdmin,则不需要以这种方式自动加载,您只需要在文件顶部require 'activeadmin'

    < / LI>
  • 如果它是您应用的常量,则需要将其放在web_app/admin.rb中,或者您需要一个包含它的文件。

我希望这可以帮助你,否则写评论,我会尽力帮助你。

答案 1 :(得分:0)

我可能有点晚了,但我找到了一个对我有用的解决方案,我认为可以适用于面临这个问题的每个人。它非常hacky,但直接到了问题的地步......

我正在使用Rails 5,我的引擎名为MyEngine,我使用默认的ActiveAdmin命名空间:admin。

转到/ admin路径时遇到的错误是:

路由错误 未初始化的常量MyEngine :: Admin

问题是它正在我的可安装引擎的命名空间中查找Admin,因为它们的工作方式,它们将所有内容封装在其命名空间中,但管理命名空间存在于根目录中(我使用rails检查控制台),所以我只是去了我的引擎的routes.rb文件,并在最后(在MyEngine :: Engine.routes.draw之后做....结束块)我刚刚添加了这一行:

MyEngine :: Admin = Admin

完成此操作后,它才有效!