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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top