我试图在权威策略中集中认证而不是在控制器中进行认证。它运行良好,但我在定制重定向和Flash消息方面失去了一些灵活性。Pundit:在一个用户动作中自定义重定向
我该如何将有关哪个认证未传递给Pundit :: NotAuthorizedError救援功能的信息?一个动作可以有两个步骤的认证:1. user.paid? 2. user.is_allowed_to_update?我想为每个案例定制消息和重定向。
exception.query
解决方案不起作用,因为它只允许为每个操作自定义闪存和重定向,而不是在一个操作中。
下面的情况更详细的解释
WITHOUT PUNDIT
Comment_Controller
def update
if user.didnt_pay?
flash[:message] = nice_message
redirect_to payment_page_path
elsif user.is_not_allowed_to_perform_action
flash[:message] = less_nice_message
redirect_to dashboard_path
end
end
现在
WITH PUNDIT
Comment_Controller
def update
authorize @comment
end
Comment_policy
def update?
user.paid? && user_is_allowed_to_perform_action
end
ApplicationController
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def user_not_authorized
flash[:message] = one_message_for_all_error # THIS IS WHAT I WANT TO CUSTOMIZE
redirect_to one_path_for_all_error # THIS IS WHAT I WANT TO CUSTOMIZE
end
我刚刚提到了一个定制评论的情况;添加自定义“重定向”路径的情况经过必要的修改。 – geekazoid