无法将devise_token_auth与active_admin一起使用

时间:2017-11-17 08:07:57

标签: ruby-on-rails devise activeadmin rails-api devise-token-auth

我正在尝试将devise_token_auth与active_admin一起使用。运行rails g active_admin:install User时出现以下错误。

错误

  

USR /本地/ LIB /红宝石/宝石/ 2.4.0 /宝石/ ActionPack的-5.1.4 / LIB / action_dispatch /路由/ route_set.rb:578:在   add_route': Invalid route name, already in use: 'new_user_session' You may have defined two routes with the same name using the:as`   选项,或者您可能正在覆盖已由资源定义的路由   具有相同的命名。

的routes.rb

Rails.application.routes.draw do
  devise_for :users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  mount_devise_token_auth_for 'User', at: 'auth'

  scope module: 'api' do
    namespace :v1 do
      resources :users, only: [:index, :show]
    end
  end

  get '/docs' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
end

2 个答案:

答案 0 :(得分:1)

你可以通过定义两个控制器来尝试不同的方法:一个用于api而另一个用于 /** * Global system settings, containing preferences that always apply identically * to all defined users. Applications can read these but are not allowed to write; * like the "Secure" settings, these are for preferences that the user must * explicitly modify through the system UI or specialized APIs for those values. */ public static final class Global extends NameValueTable {

active_admin

现在从# app/controllers/api_controller.rb # API routes extend from this controller class ApiController < ActionController::Base include DeviseTokenAuth::Concerns::SetUserByToken end # app/controllers/application_controller.rb # leave this for ActiveAdmin, and any other non-api routes class ApplicationController < ActionController::Base end 的{​​{1}}和ApiController继承所有api控制器。

There is a known issue between ActiveAdmin and DeviseTokenAuth

答案 1 :(得分:1)

我通过将mount_devise_token_auth_for 'User', at: 'auth' 移动到api范围来实现它。答案是对的,here

Rails.application.routes.draw do
  devise_for :users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)

  constraints subdomain: 'api'do
    scope module: 'api' do
      namespace :v1 do
        resources :users, only: [:index, :show]
        mount_devise_token_auth_for 'User', at: 'auth'
      end
    end

    get '/docs' => redirect('/swagger/dist/index.html?url=/apidocs/api-docs.json')
  end
end