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
rails_admin.rb:
RailsAdmin.config do |config|
config.authorize_with :cancan
end
user.rb
class User < ActiveRecord::Base
. . . . . .
. . . . . .
ROLES = %w[admin, moderator, user]
def role?(requested_role)
self.role == requested_role.to_s
end
end
rails c:
irb(main):001:0> @user = User.first
irb(main):002:0> @user.role?(:admin)
=> true
Когда пытаюсь из под этого пользователя зайти на /admin пишет You are not authorized to access this page.
Как исправить?