好问题。我在我的项目中使用Rails Admin和Pundit。
我更喜欢将Admin模型与User模型分开。
其中一个原因是我喜欢能够从Rails Admin“成为用户”,以便在出现问题时能够帮助他们。当您拥有单独的用户和管理员模型时,更容易做到这一点。
管理模式可以非常简单。与 rails generate devise Admin
然后在你的config/initializers/rails_admin.rb
生成它添加
config.authenticate_with do
warden.authenticate! :scope => :admin
end
config.current_user_method(&:current_admin)
重定向到正确的配置文件,此方法添加到您的ApplicationController
def after_sign_in_path_for(resource)
if resource.class == Administrator
rails_admin_path
else
# Change profile_path to where you want regular users to go
stored_location_for(resource) || profile_path
end
end
为了防止从目前的注销管理员从当前用户注销时,在config/initializers/devise.rb
中设置此配置
config.sign_out_all_scopes = false
为了解决您的其他问题,我使用了CanCan和Pundit。我更喜欢Pundit,因为CanCan会为每个请求评估所有权限。借助Pundit,只有在需要时才能检查权限。根据我的经验,专家也更加灵活。
@Monkbroc嗨只是好奇你是如何实现的权威人士多用户类(如:管理员和用户) – jufemaiz 2015-05-12 00:59:39
当然@jufemaiz。你可以发表一个关于多个模型的专家的问题,我会回答吗? – monkbroc 2015-05-13 01:46:01
会做@monkbroc – jufemaiz 2015-05-13 02:31:30