我正在使用 Devise + CanCan 并希望重定向到我的管理界面(由 rails admin gem 生成)如果它是登录的管理员..
我使用以下内容自定义了用户重定向到他们各自的配置文件:
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, :alert => exception.message
end
def after_sign_in_path_for(resource)
user_path(current_user)
end
end
角色.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
end
能力.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
can :access, :rails_admin
can :dashboard
end
用户.rb
class User < ActiveRecord::Base
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
.....