7

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

lbz
  • 9,560
  • 2
  • 30
  • 35
daniel
  • 3,105
  • 4
  • 39
  • 48

2 Answers2

14

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
Sinetris
  • 8,288
  • 2
  • 22
  • 17
  • @Sinetris thank you sir. I have two questions: (i) what does the double exlamation marks mean "!!" and (ii) if I"m in the user console, then User.roles should come up with an active record collection, is this correct? thank you for your post – BenKoshy Jul 06 '16 at 05:37
1

You should refer to this:

https://github.com/ryanb/cancan/wiki/Role-Based-Authorization

I think it'll be really helpful..

d34th4ck3r
  • 719
  • 2
  • 11
  • 38