4

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

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

1 に答える 1

4

adminUserモデルにブール属性があると仮定します。

def after_sign_in_path_for(resource)
  if current_user.has_role? :admin
    rails_admin.dashboard_path
  else
    user_path(current_user)
  end
end
于 2012-05-31T05:55:56.173 に答える