Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CanCan - "user" parameter is nil - Rails 4

I am using Rails 4.0.1 and ruby 2.1.1. I am trying to integrate cancan with rails_admin. My application_controller.rb looks like this

helper_method :current_user

private

def current_user
  @current_user ||= User.find_by(email: session[:user_id]) if session[:user_id]
end 

My current_user helper method is working fine from rest of the application. My ability.rb looks like this

class Ablity
  include CanCan::Ability

   def initialize(user)
     if user
       can :access, :rails_admin
     end
   end
end

Control is not going inside the if condition at all, which means the "user" parameter is "nil". When I try to access rails_admin I get a CanCan::AccessDenied exception. Where am I going wrong?

Edit : I am not using devise for authentication

Update : I've replaced cancan with cancancan version 1.8. Still not working

like image 755
Srivathsa Avatar asked Dec 25 '22 09:12

Srivathsa


1 Answers

Solved. In config/initializers/rails_admin.rb we have to specify the current_user_method as

config.current_user_method { current_user } # refers to the current_user helper method in my case

Now everything is working perfectly. I am surprised why this isn't specified anywhere in the documentation.

like image 138
Srivathsa Avatar answered Dec 27 '22 23:12

Srivathsa