简体   繁体   English

如何从我的ability.rb中指定自定义异常消息?

[英]How do I specify a custom exception message from my ability.rb?

In my ability.rb , I have the following rule: 在我的ability.rb ,我有以下规则:

elsif user.has_role? :demo
  can :read, Profile, demo_featured: true, demo_linked: true, message: "To access this profile, please subscribe here."

But that doesn't produce the message I want. 但那不会产生我想要的信息。

How do I get this specific rule to produce the message I want? 如何获得此特定规则以生成我想要的消息?

Edit 1 编辑1

Here is the full ability.rb if condition: 这是完整的ability.rb if条件:

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    alias_action :create, :show, :new, :destroy, to: :csnd

    if user.has_role? :admin
      can :manage, :all
    elsif user.has_role? :coach
      # do some stuff
    elsif user.has_role? :demo
      can :read, Profile, demo_featured: true, demo_linked: true
    elsif user.has_role? :player
      # can do some stuff
    else
      can :read, Profile
    end    
  end

These are some bits from my ProfilesController : 这些是我的ProfilesController中的一些内容:

  before_action :set_profile, only: [:show, :edit, :update, :destroy, :invite_user, :profiles]

    def set_profile
      @profile = Profile.published.includes(:grades, :positions, :achievements, :videos, :transcripts).friendly.find(params[:id])
    end

The cancan docs give examples of customizing the message when you authorize! cancan文档提供了在您authorize!时自定义消息的示例authorize! in the controller, and when you manually raise an error, but there doesn't seem to be any mechanism for specifying messages in ability.rb . 在控制器中,当您手动引发错误时,似乎没有任何机制可以在ability.rb指定消息。

Instead, you could catch and modify it in your ApplicationController : 相反,您可以在ApplicationController捕获并修改它:

class ApplicationController < ActionController::Base
  rescue_from CanCan::AccessDenied do |exception|
    if current_user.has_role? :demo
      redirect_to :back, :alert => "To access this profile, please subscribe here."
    end
    # render 403, etc.
  end
end

Look for rescue_from CanCan::AccessDenied in your main application controller or in your specific controller. 在主应用程序控制器或特定控制器中查找rescue_from CanCan::AccessDenied It should do something like redirecting to a login page. 它应该像重定向到登录页面。 In my case it's something like this: 就我而言,它是这样的:

rescue_from CanCan::AccessDenied do ||
  redirect_to new_user_session_path
end

Since you're producing a different exception message and then displaying it it'd probably be like this, using the flash: 既然你正在生成一个不同的异常消息然后显示它,它可能是这样的,使用flash:

rescue_from CanCan::AccessDenied do |exception|
  flash[:notice] = exception.message
  redirect_to new_user_session_path
end

Your own logic may vary depending on how you want to handle when the user doesn't have access. 您自己的逻辑可能会有所不同,具体取决于您在用户无权访问时的处理方式。 It's possible you may even have it setup in a per-controller basis, but this should be the gist of it. 您可能甚至可以在每个控制器的基础上进行设置,但这应该是它的要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM