0
votes

Please, give me any idea about a little problem: in my Rails 3 app I need current_user always be NOT nil. I'm using Devise and CanCan for auth system and I thinking about how to implement "guest" user. For roles like "admin" it works fine ( if current_user.is? :admin), but if user is not logged in I wish to check not user_signed_in?, but the same way (if current_user.is? :guest) for example.

I think I need to put somthing creating current_user object ib before_filters of Application_Controller, but I dont' know how to create this global thing right way.

Thanks for any answers!

2

2 Answers

2
votes

You could try it like this:

if current_user.try(:is?, :admin)

So it won't matter if current_user is nil

1
votes

If you are using 'CanCan' you should be adding functionality like you mentioned into you Abilities class. For example:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    can :manage, :all if user.is? :admin
    can :read, :all if user.is? :guest
  end
end

For more information take a look through the Wiki or watch the screen cast: