Dylan Taylor
Dylan Taylor

Reputation: 69

Cancan and devise not working

I've modified my devise table, User, to have a clearance column. This column is a number (1-3) that represents a user's permissions (read, read/write, full control). Unlike all the examples I'm reading, my Clearance (Role in the examples) is not a separate table with a relationship but is its own column in the Devise table (User). Its default value is 1.

My ability.rb looks like this:

 class Ability
 include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.clearance.equal?("2")
      can :create, Post
      can :manage, Post, :user_id => user.id
    else
      can :read, :all
    end
  end

end

I've written this off of other examples I've seen (I'm really new to Rails but trying to not ask for help unless absolutely necessary) with the intent that users with a clearance value of 2 can create posts and manage only their posts. I've also included that, because I haven't written the code for 1 and 3 yet, that all other clearance numbers can read everything.

Apperantly, however, Cancan thinks that my user (confirmed clearance level of 2) falls under the "else" provision and therefore I can only read posts. I get a You are not authorized to access this page message if I try to make a new one. I'm lost. Help?

Upvotes: 0

Views: 474

Answers (1)

davidrac
davidrac

Reputation: 10738

May this be that the clearance column is an integer? in such a case you should write user.clearance == 2 and not as you wrote.

Upvotes: 1

Related Questions