是否有一种 Rails 模式可以为不同类型的用户提供不同的主页?
Is there a Rails pattern for giving different kinds of users different home pages?
我最初的想法是只在 User
模型上有一个 home_page
属性,然后在验证后做一个 redirect_to
。就这么简单,还是有更好的方法?
作为背景,这里有一些用例:
一个用户可以是管理员,并且可以冒充站点上的其他用户以提供支持。我们的管理员这样做是为了“看到”客户看到的内容。登录时,管理员将被定向到一个页面,该页面显示当前使用情况统计信息以及应用程序中发生的任何错误。
另一类用户是来自合作伙伴市场的合作者。假设该公司名为 Amazing。 Amazing 的经理可以登录并查看在 Amazing 市场上销售产品的所有用户,但他没有与管理员相同的访问权限。我想将此用户重定向到显示 Amazing marketplace 上所有卖家的页面。
标准客户将被定向到仪表板页面,该页面将帮助他们管理市场上的广告。
如果使用Devise,您可以为用户区分主页。在一个旧项目上我做了这样的事情,代码不是很干净,但你可以得到一个想法:
routes.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
我最初的想法是只在 User
模型上有一个 home_page
属性,然后在验证后做一个 redirect_to
。就这么简单,还是有更好的方法?
作为背景,这里有一些用例:
一个用户可以是管理员,并且可以冒充站点上的其他用户以提供支持。我们的管理员这样做是为了“看到”客户看到的内容。登录时,管理员将被定向到一个页面,该页面显示当前使用情况统计信息以及应用程序中发生的任何错误。
另一类用户是来自合作伙伴市场的合作者。假设该公司名为 Amazing。 Amazing 的经理可以登录并查看在 Amazing 市场上销售产品的所有用户,但他没有与管理员相同的访问权限。我想将此用户重定向到显示 Amazing marketplace 上所有卖家的页面。
标准客户将被定向到仪表板页面,该页面将帮助他们管理市场上的广告。
如果使用Devise,您可以为用户区分主页。在一个旧项目上我做了这样的事情,代码不是很干净,但你可以得到一个想法:
routes.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