Answer the question
In order to leave comments, you need to log in
Why cancan not let me on the admin page?
Ability.rb:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
can :read, :all
if user && user.role?(:admin)
can :access, :rails_admin # only allow admin users to access Rails Admin
can :dashboard
can :manage, :all
elsif user.role?(:user)
can [:read, :create], :all
can [:update], Post, user_id: user.id
can [:update, :destroy], Comment, user_id: user.id
can :read, :all
elsif user.role?(:moderator)
can :manage, :all
end
end
end
RailsAdmin.config do |config|
config.authorize_with :cancan
end
class User < ActiveRecord::Base
. . . . . .
. . . . . .
ROLES = %w[admin, moderator, user]
def role?(requested_role)
self.role == requested_role.to_s
end
end
irb(main):001:0> @user = User.first
irb(main):002:0> @user.role?(:admin)
=> true
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question