Невозможно использовать devise_token_auth с active_admin

Я пытаюсь использовать devise_token_auth с active_admin. При запуске rails g active_admin:install User я получаю следующую ошибку.

ошибка

usr/local/lib/ruby/gems/2.4.0/gems/actionpack-5.1.4/lib/action_dispatch/routing/route_set.rb:578:in add_route': Invalid route name, already in use: 'new_user_session' You may have defined two routes with the same name using the:as`, или вы можете переопределить уже определенный маршрут ресурсом с тем же именем.

маршруты.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

person Antarr Byrd    schedule 17.11.2017    source источник
comment
не могли бы вы поделиться выводом rails routes -g new_user_session?   -  person Wasif Hossain    schedule 17.11.2017
comment
@WasifHossain Я получаю ту же ошибку, что и выше. Если я удалю devise_for, я смогу запустить сервер, но не смогу войти в active_admin   -  person Antarr Byrd    schedule 17.11.2017


Ответы (2)


Вы можете попробовать другой подход, определив два контроллера: один для API, а другой для 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

Теперь наследуйте все контроллеры API от ApiController и ActiveAdmin от ApplicationController.

Существует известная проблема между ActiveAdmin и DeviseTokenAuth< /а>

person Wasif Hossain    schedule 17.11.2017
comment
я понимаю. позвольте мне глубоко погрузиться в это - person Wasif Hossain; 17.11.2017

Я заработал, переместив mount_devise_token_auth_for 'User', at: 'auth' в область API. Ответ был правильным, здесь.

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
person Antarr Byrd    schedule 17.11.2017
comment
точно! так и надо делать. спасибо и за решение! - person Wasif Hossain; 17.11.2017