当我重定向到正确的页面时,我试图让 CanCan 显示异常消息。我正在按照以下application_controller
代码处理我的异常:
rescue_from CanCan::AccessDenied do |exception|
if current_user # probably not an admin
redirect_to dashboard_index_path, :notice => exception.message
else
redirect_to hello_login_path, :notice=> exception.message
end
end
但是,当我以用户身份注销(这会触发上述页面的 else 部分)然后尝试访问一个页面时,我不应该有权访问它只是将我重定向到 hello_login_path 而不显示异常消息。Par example,当我尝试转到dashboard#index
具有以下权限的 时:
class DashboardController < ApplicationController
def index
#we want to list out all the the projects
@projects = Project.all
authorize! :read, @projects, :message => 'You are not logged in'
@entries = Entry.all
authorize! :read, @entries, :message => 'You are not logged in'
@users = User.all
authorize! :read, @users, :message => 'You are not logged in'
#@entries = Entry.where(:user_id => params[:user_id])
#authorize! :read, @entries
end
end
它有效地将我重定向到该hello#login
页面,但不显示“您未登录”消息。
谢谢你的帮助!