具有 rails API 应用程序错误的 activeadmin:Admin::DashboardController#index 中的 ArgumentError

activeadmin with rails API app errors: ArgumentError in Admin::DashboardController#index

我正在尝试合并 active admin with a rails_api 应用程序。

详情:

因为它是 API,没有 cookie 和其他东西,所以我使用的是 devise_auth_token for authentication ( not devise)。

身份验证工作正常。但是,我现在正在尝试将 active admin 与此合并,并按照以下步骤操作:

gem 'activeadmin', '1.0.0.pre2'
$ bundle install
$ rails g active_admin:install 
$ rake db:migrate db:seed
$rails server

在前往 http://localhost:3000/admin 时,出现以下错误:

ArgumentError in Admin::DashboardController#index
wrong number of arguments (given 1, expected 0)

指向一行:define_method(name) do 在:

 if instance_reader
    remove_possible_method name
    define_method(name) do
      if instance_variable_defined?(ivar)
        instance_variable_get ivar
      else

但是请注意,直接访问 http://localhost:3000/admin/login 会打开登录页面,但登录后会重定向回 http://localhost:3000/admin 具有相同的错误消息。

Gemfile:

gem 'pg'
gem 'devise'
gem 'devise_token_auth'
gem 'omniauth'
gem 'money-rails'
gem 'swagger-docs'
gem 'activeadmin', '1.0.0.pre2'gem 'pg'
gem 'devise'
gem 'devise_token_auth'
gem 'omniauth'
gem 'money-rails'
gem 'swagger-docs'
gem 'activeadmin', '1.0.0.pre2'

Routes.rb:

Rails.application.routes.draw do
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  namespace :api do
    namespace :v1 do
      mount_devise_token_auth_for 'User', at: 'auth'
      ...
    end
  end
  root to: "home#index"
end

依赖关系:

堆栈跟踪:

ArgumentError (wrong number of arguments (given 1, expected 0)):
  activesupport (4.2.4) lib/active_support/core_ext/class/attribute.rb:106:in `block (2 levels) in class_attribute'
  devise_token_auth (0.1.37) app/controllers/devise_token_auth/concerns/set_user_by_token.rb:21:in `set_user_by_token'
  devise_token_auth (0.1.37) lib/devise_token_auth/controllers/helpers.rb:117:in `current_admin_user'
  devise_token_auth (0.1.37) lib/devise_token_auth/controllers/helpers.rb:105:in `authenticate_admin_user!'
  activeadmin (1.0.0.pre2) lib/active_admin/base_controller.rb:39:in `authenticate_active_admin_user'
  activesupport (4.2.4) lib/active_support/callbacks.rb:432:in `block in make_lambda'
  activesupport (4.2.4) lib/active_support/callbacks.rb:164:in `block in halting'
  activesupport (4.2.4) lib/active_support/callbacks.rb:504:in `block in call'
  activesupport (4.2.4) lib/active_support/callbacks.rb:504:in `each'
  activesupport (4.2.4) lib/active_support/callbacks.rb:504:in `call'
  activesupport (4.2.4) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.4) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.4) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.4) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.4) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.4) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:45:in `serve'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/flash.rb:260:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `catch'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.4) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
  railties (4.2.4) lib/rails/engine.rb:518:in `call'
  railties (4.2.4) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /Users/Sunday/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
  /Users/Sunday/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
  /Users/Sunday/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'

我怀疑这可能与 rails_api 默认不包含几个 rails 中间件有关...但实际上,我不知道它是什么,或者我该怎么做。请任何人,我需要这方面的帮助。

我的组合是否有效?

这绝对不是一个很好的组合。

Rails API 是 Rails 的缩小版本,适用于没有普通 Web 界面的应用程序 - 它更快,因为它删除了需要的组件经典网络应用程序,例如会话。

这些组件称为中间件。

ActiveAdmin 是一个快速简单的 Web 界面,用于对您的模型进行 CRUD。这样人们就可以通过单击按钮而不是使用 API.

来编辑数据

您可能想要做的是拥有两个单独的 rails 应用程序 - 一个通过 API 提供数据,另一个让人们使用 ActiveAdmin 的网络应用程序。当您需要网络应用程序但不希望它给您的 API.

带来负担时,这是一个相当常见的解决方案

每个都将存在于自己的 GIT 存储库中,并由不同的服务器提供服务。您可以通过将模型提取到 gem(s).

来共享组件

这是将 devise_auth_tokenactive_admin

一起使用时出现的问题

这是 devise_token_auth gem 本身给出的解决方法。

出于某些奇怪的原因,ActiveAdmin 是从您自己的应用程序的 ApplicationController 扩展而来的。如果您在应用程序的 ApplicationController 中包含 DeviseTokenAuth::Concerns::SetUserByToken 问题,这就会成为一个问题。

解决方案是使用两个单独的 ApplicationController 类 - 一个用于您的 API,另一个用于 ActiveAdmin。像这样:

app/controllers/api_controller.rb

API 路由从此控制器延伸

class ApiController < ActionController::Base
  include DeviseTokenAuth::Concerns::SetUserByToken
end

app/controllers/application_controller.rb

将此留给 ActiveAdmin,以及任何其他非api 路由

class ApplicationController < ActionController::Base
end

This is the reference