使用 CanCan 进行授权,使用 Devise 进行身份验证。我有三个角色。管理员角色有权访问所有操作。但是当我以管理员身份登录时,我仍然得到拒绝访问。我究竟做错了什么??我也一直在关注这个 wiki 进行实施
这是我的控制器代码
class LocationController < ApplicationController
#before_filter :authenticate, :only => [:title_clearing]
before_filter :authenticate_user!
load_and_authorize_resource
def show
@data = []
if params[:Date].match(/^(19|20)\d\d[- \/.](0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])$/).nil? == true
@message = "Wrong Date Format"
else
@data = Location.show(params[:Date])
if @data.size == 0
@message = "No data exists for "+ params[:Date]
else
@message = "Data loaded successfully for "+ params[:Date]
end
end
if params[:Date] == "all"
@message = "All records loaded"
@data = Location.show("all")
end
end
end
在能力.rb
if user.is? :admin
can :all, :location
end
if user.is? :titleclearing
can :title_clearing, :location
end
if user.is? :acquisitions
cannot :title_clearing, :location
end
在用户模型中
def role?(role)
roles.include? role.to_s
end