Question

I'm having a hard time understanding how CanCan works. I have the following model

class Ability
  include CanCan::Ability
  def initialize(user)
    if user && user.email == "jason@gmail.com"
      can :access, :rails_admin       # only allow admin users to access Rails Admin
      can :dashboard                  # allow access to dashboard
    end
  end
end

When it comes to my rails_admin file in the initializers folder

RailsAdmin.config do |config|
  config.authorize_with :cancan

  config.main_app_name = ['Pr', 'Admin']

  config.current_user_method { } # auto-generated
end

I want to have one user to access the admins dashboard with the email "jason@gmail.com", but how does CanCan know who is currently signed in at the time? Does it rely on a helper method I'm missing?

Was it helpful?

Solution

CanCan uses a current_ability method to supply the ability, and in that it uses current_user. I know at least Devise has this method, other auth frameworks must commonly supply it too, not sure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top