1

我创建了一个 rails (6.0.3) 应用程序,并添加了thinkbot/administratedevise。我有一个User普通用户的模型和另一个AdminUser. 我想将两种类型的用户分开。两者都是用设计生成器创建的

$ rails generate devise User
$ rails generate devise AdminUser

我在管理和设计范围的路由配置方面遇到问题。

Rails.application.routes.draw do
  devise_for :users
  devise_scope :user do
    get 'log-in', to: 'devise/sessions#new'
    get 'sign-up', to: 'devise/registrations#new'
    delete 'log-out', to: 'devise/sessions#destroy'
    get 'reset-password', to: 'devise/passwords#new'
  end

  root 'pages#index'

  get '/dashboard', to: 'pages#dashboard'

  namespace :admin do
    devise_for :admin_users

    resources :users
    resources :groups

    root to: 'users#index'
  end
end

我想我错过了一些东西,因为当我转到localhost:3000/admin它时它会抛出异常并重定向到http://localhost:3000/admin/admin_users/sign_in. 这里有一个例外:

Started GET "/admin" for ::1 at 2021-02-13 11:58:13 -0300
Processing by Admin::UsersController#index as HTML
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms | Allocations: 207)


Started GET "/admin/admin_users/sign_in" for ::1 at 2021-02-13 11:58:13 -0300

ActionController::RoutingError - uninitialized constant Admin::SessionsController:

Started POST "/__better_errors/6ee21cb1da542538/variables" for ::1 at 2021-02-13 11:58:13 -0300

管理应用程序控制器如下所示:

module Admin
  class ApplicationController < Administrate::ApplicationController
    before_action :authenticate_admin_user!

    protected

    def authenticate_admin_user!
      authenticate_admin_admin_user!
    end
  end
end

LMK 如果我错过了一些有用的东西。提前致谢

4

1 回答 1

2

该错误告诉您必须定义控制器。

admin/session_controller.rb

因此,当您使用与普通用户不同的会话时,创建一个从设计会话扩展的控制器,因此您也必须定义管理员用户控制器。

于 2021-02-16T07:19:14.393 回答