0

I'm trying to figure out how to define cancancan abilities in my rails 4 app.

I use devise for authentication.

When I try:

  if user_signed_in?
        can :crud, Profile, :user_id => user.id #[for themselves]

I get an error that says:

undefined method `user_signed_in?' for #<Ability:0x007fd01b6d2fc0>

I thought devise had a helper called user_signed_in?

I don't understand this error at all. Can anyone see what I have done wrong?

Mel
  • 2,481
  • 26
  • 113
  • 273
  • Why are you trying to use the user_signed_in? helper in Ability.rb? The way to differentiate between users who are logged in and not logged in is that users who are logged in have roles. – MarsAtomic Sep 03 '15 at 01:34
  • No. Users can be assigned roles. It is possible that a user is signed in and does not yet have a role assigned. That's why I want to use the devise help to check if user_signed_in? (which I thought was supposed to work with cancancan). Am I supposed to define another role in my model as a default for registered users who have not yet been assigned a role? – Mel Sep 03 '15 at 01:43
  • 1
    Helper methods are available in views and controllers. Ability.rb sits on the model side of MVC. There are ways to make helpers available to models, but you should be passing parameter to models for a clean and proper approach. It does sound like you should have a "role_pending" role or something like that to do what you like. CanCanCan differentiates between signed in (registered user) and not signed in (guest) with this line from Ability.rb: `user ||= User.new # guest user (not logged in)` – MarsAtomic Sep 03 '15 at 01:48
  • I see. Thank you. I didn't know I couldn't use the helper in a model. Thanks very much – Mel Sep 03 '15 at 01:50

0 Answers0