Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CanCan error 'undefined method role?' with Devise

Hey I hope you can help me:

I was going through this tutorial

http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/ and I did every step by step.

I wasnt quite sure where to put the role?-method from the tutorial, because it doesnt say where to place it.

Now it gives me this error when I want to sign_up or Sign_in

Many thanks

like image 970
daniel Avatar asked Jan 12 '11 23:01

daniel


1 Answers

You need to add it in the user model (app/models/user.rb)

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles
  devise :database_authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable

  def role?(role)
      return !!self.roles.find_by_name(role.to_s.camelize)
  end
end
like image 168
Sinetris Avatar answered Sep 17 '22 18:09

Sinetris