Search code examples
ruby-on-railsrolify

Rolify abilities returning false


I'm experiencing a strange issue using Rolify (click for tutorial I'm following) with Rails: The can method does not seem to work, therefore rendering user privileges unuseable. Below is my ability.rb file and console output where the problem is demonstrated.

class Ability
  include CanCan::Ability

  def initialize(user)

    user ||= User.new # guest user (not logged in)

    if user.has_role? :admin
      can :manage, :all
    else
      can :read, :all
    end

  end
end

Console tests ($ rails console)

user = User.find(2)
user.add_role "admin"
user.has_role? :admin
=> **true**

ability = Ability.new(user)
ability.can? :manage, :all
=> **false**
ability.can? :read, :all
=> **false**

I also checked in the database and all the relationships are set up correctly. I'm running rails 3.2.13.


Solution

  • The problem was a gem conflict with either canard or declarative_authorization. Disabling both and restarting the rails server solved the issue. Perhaps this will help others who have gone down the same path in trying these different gems.