我创建了一个rails(6.0.3)应用程序,并添加了thoughtbot/administration和design。我有一个普通用户的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。提前感谢
错误告诉你必须定义你的控制器
admin/session_controller.rb
所以创建一个控制器,从设计会话扩展,因为你使用会话不同的普通用户,所以你必须定义admin用户控制器。