1

我正在尝试将 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
4

2 回答 2

1

您可以通过定义两个控制器来尝试不同的方法:一个用于 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和.ActiveAdminApplicationController

ActiveAdmin 和 DeviseTokenAuth 之间存在一个已知问题

于 2017-11-17T08:27:32.080 回答
1

我通过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
于 2017-11-17T08:43:46.183 回答