0

I have Rails 4 with Role Model for role assignment.

I can't figure out how to remove a role that was assigned to a user.

I have two roles in my profile.rb, :manager and :student.

I have a user that has been assigned both roles. I want to remove manager from that user's role assignment.

It works to try p.roles << :student, to add a role, so I tried p.roles >> :manager to try deleting it, but it didn't work.

How do you delete a role from a user?

Also, in my view, when I do:

 if(current_user.has_role? :student)

I get this error:

undefined method `has_role?' for #<User:0x007fb3b39dacb0>

I don't know or understand what this message means.

Any help would be appreciated. Thanks

Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

0

You can remove a role from a user with the method remove_role. Let's say you want to remove the :manager role from a user with ID = 1:

user = User.find(1)
user.remove_role :manager

For the second question, which is the model that you assign roles with? Profile or User? You an only call Rolify methods directly to the model that you specified "Rolify" like this:

class User < ActiveRecord::Base
  rolify
end

Or if you are using role_model gem :

user.valid_roles.delete(:manager)
Raphael Ottoni
  • 506
  • 3
  • 14