Can cancan be used with many resources?

I use devise and created two models: Doctor and Patient. When I write load_and_authorize_resource cancan gives an exception

 undefined local variable or method `current_user 

How can I use cancan for both of them?

+4
source share
2 answers

There is a method in the application controller called current_ability , you can just overwrite it.

Change current documentation

It will look something like this.

 def current_ability @current_ability ||= Ability.new(current_user) end 

But you can change it to

 def current_ability if current_doctor @current_ability ||= Ability.new(current_doctor) else @current_ability ||= Ability.new(current_patient) end end 

This will allow you to initialize the method in app / models / .rb ability to get either current_patient or current_doctor depending on the case. You can customize it to look something like this.

 class Ability include CanCan::Ability def initialize(resource) if resource.class == Patient can, Read Something can, Edit Other . . . else can, Read Something can, Edit Other . . . end . . . 

I hope this help, you can also see cancan Full document

+4
source

Here you can find the full configuration of Devise and Cancan: Part 1 Part 2

0
source

Source: https://habr.com/ru/post/1342176/


All Articles