我正在尝试将active admin与rails_api应用合并。
详细信息:
因为它是一个API,没有任何Cookie和内容,我使用devise_auth_token进行身份验证(而不是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
in:
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中间件的事实有关......但实际上,我不知道它是什么,或者我怎么能去做。请任何人,我需要帮助。
我的组合甚至可以工作吗?
答案 0 :(得分:11)
将devise_auth_token
与active_admin
这是devise_token_auth
宝石本身给出的解决方法。
出于某种奇怪的原因,ActiveAdmin从您自己的应用程序的ApplicationController扩展而来。如果您在应用程序的ApplicationController中包含DeviseTokenAuth :: Concerns :: SetUserByToken问题,则会出现问题。
解决方案是使用两个单独的ApplicationController类 - 一个用于API,另一个用于ActiveAdmin。这样的事情:
class ApiController < ActionController::Base
include DeviseTokenAuth::Concerns::SetUserByToken
end
class ApplicationController < ActionController::Base
end
答案 1 :(得分:2)
这绝对不是一个非常好的组合。
Rails API是Rails的缩小版本,适用于没有普通Web界面的应用程序 - 它更快,因为它会删除经典Web应用程序(如会话)所需的组件。
这些组件称为中间件。
ActiveAdmin是一个快速简便的Web界面,用于在模型上进行CRUD。这样人类就可以通过点击按钮而不是使用API来编辑数据。
您可能想要做的是拥有两个独立的rails应用程序 - 一个通过API提供数据的应用程序和一个允许人们使用ActiveAdmin的Web应用程序。当您需要一个Web应用程序但不希望它降低您的API负担时,这是一个相当常见的解决方案。
每个人都会住在自己的GIT存储库中,并由不同的服务器提供服务。您可以通过将模型提取到gem(s)中来共享模型等组件。