如果使用
Devise
。在一个旧项目上,我做了这样的事情,代码不是很干净,但你可以得到一个想法:
路线.rb
Rails.application.routes.draw do
mount Ckeditor::Engine => '/ckeditor'
devise_for :admins,
:frontend_operators,
:backend_operators,
class_name: 'User',
path: '',
controllers: {sessions: 'frontend/auth/sessions',
confirmations: 'frontend/auth/confirmations',
unlocks: 'frontend/auth/unlocks',
passwords: 'frontend/auth/passwords',
registrations: 'frontend/auth/registrations'},
path_names: {sign_in: 'login',
sign_out: 'logout',
password: 'secret',
confirmation: 'verification',
sign_up: 'register',
edit: 'profile/edit'}
...
authenticated :backend_operator do
get :backend_operator_root, action: :index, controller: "backend/home"
end
authenticated :frontend_operator do
get :frontend_operator_root, action: :index, controller: "frontend/home"
end
authenticated :admin do
get :admin_root, action: :index, controller: "backend/home"
end
end
application_controller.rb
class ApplicationController < ActionController::Base
devise_group :user, contains: [:backend_operator, :frontend_operator, :admin]
[:backend_operator, :frontend_operator, :admin].each do |model|
define_method("decorated_current_#{model}") { send("current_#{model}").decorate }
helper_method "decorated_current_#{model}".to_sym
end
before_action :set_root, unless: :root_present?
protected
def set_root
Rails.application.config.root_url = root_url
end
end