2
我有一個用Devise創建的用戶模型,它有一個admin布爾值字段和一個角色整數字段。我正在嘗試創建一個能力類,允許非管理員用戶具有基於角色的某些能力,但是在此過程中遇到問題。我已經創建了一個授權控制器這樣:授權用戶使用cancan和devise
class AuthorizedController < ApplicationController
before_filter :authenticate_user!
check_authorization :unless => :devise_controller?
load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
flash[:alert] = exception.message
redirect_to root_url
end
end
class Ability
include CanCan::Ability
def initialize(user)
if !user
can :read, :all
end
if user
admin_rules if user.admin?
commenter_rules if user.role.equal?("1")
author_rules if user.role.equal?("2")
end
end
def admin_rules
can :manage, :all
end
def commenter_rules
can :manage, Data, :active => true, :user_id => user.id
end
def author_rules
can :manage, Post, :active => true, :user_id => user.id
end
end
現在每當我試圖訪問任何網頁,它會拋出異常消息「您無權訪問此頁面。」而不是能夠訪問該網站的那一部分。
你能和發佈錯誤回溯? – maxenglander
來自Devise的':active'屬性在哪裏? – janders223
沒有實際的例外信息難以幫助 –