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

标签 ruby-on-rails ruby-on-rails-5 cancan cancancan

在我的 ability.rb ,我有以下规则:

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

但这不会产生我想要的信息。

如何获得此特定规则以生成我想要的消息?

编辑 1

这是完整的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

这些是我的 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

最佳答案

关于ruby-on-rails - 如何从我的 ability.rb 中指定自定义异常消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41096174/

相关文章:

ruby-on-rails - RSPEc - #<RoomsController :0x0000010534c050> 的未定义方法 `stubs'

ruby-on-rails - 从 activerecord 哈希中获取所有内容

css - 无法在简单形式选择中添加 css 类

ruby - 将 Rails 从 5.1 升级到 5.2 后,我收到以下警告消息

layout - Rails 为同一操作提供不同的 Devise Mailer 布局

ruby-on-rails - Rails wiki gem for production with devise/cancan - 非个人使用

ruby-on-rails - Wordpress 能否被 Django 或 Ruby on Rails 等框架取代?

mysql - Rails Multi-Tenancy 架构,限定对多个租户的访问

ruby-on-rails - 使用 rspec-rails 测试无数据库 Rails 5 应用程序

ruby-on-rails - 向 Rails Composer、Devise、CanCan、Twitter Bootstrap 添加角色