我最初的想法是只home_page在模型上有一個屬性User,然后進行redirect_to身份驗證。就這么簡單,還是有更好的方法?
作為背景,這里有一些用例:
一個用戶可以是管理員,并且可以冒充站點上的其他用戶以提供支持。我們的管理員這樣做是為了讓他們能夠“看到”客戶所看到的內容。登錄時,管理員將被引導到一個頁面,該頁面顯示當前使用情況統計資訊以及應用程式中發生的任何錯誤。
另一種型別的用戶是來自合作伙伴市場的合作者。假設這家公司名為 Amazing。Amazing 的經理可以登錄并查看在 Amazing 市場上銷售產品的所有用戶,但他沒有與管理員相同的訪問權限。我想將此用戶重定向到顯示驚人市場上所有賣家的頁面。
標準客戶將被引導至儀表板頁面,該頁面將幫助他們管理市場上的廣告。
uj5u.com熱心網友回復:
如果使用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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471112.html
標籤:轨道上的红宝石
上一篇:在Rubyonrails中的controller.rb中將方法名稱destroy更改為delete,
下一篇:急切地加載一個關聯,但只有一個列
