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.

有帮助吗?

解决方案

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.

其他提示

CanCan relies on existence of current_user method which would return currently logged in user. This method is there if you are you using for example devise to authenticate. If you are using something else then you have to make sure current_user method is there.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top