簡體   English   中英

設計管理員重定向

[英]Devise admin redirect

我使用Devise + CanCan並想重定向到我的管理界面(由rails admin gem生成)如果它是一個登錄的ADMIN ..

我使用以下方法將USER重定向自定義為各自的配置文件:

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

role.rb

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :users_roles
  belongs_to :resource, :polymorphic => true
end

ability.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

user.rb

class User < ActiveRecord::Base
    rolify
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
 .....

我假設您的User模型有一個布爾admin屬性。

def after_sign_in_path_for(resource)
  if current_user.has_role? :admin
    rails_admin.dashboard_path
  else
    user_path(current_user)
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM