Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Working with CanCanCan

Eron edited this page Jul 18, 2020 · 4 revisions

app/models/ability.rb

class Ability
  include Knock::Authenticable
  include CanCan::Ability

  def initialize(current_user)
    if current_user.has_role? :admin
      can :create, EligibleItem
    end
  end
end

app/controllers/application_controller.rb

class ApplicationController < ActionController::API
  include Knock::Authenticable

  rescue_from CanCan::AccessDenied do |exception|
    render json: { error: 'You are not authorized to perfom this action' }, status: :forbidden
  end
end

Result: image

Thanks! ✨

Clone this wiki locally