M
M
mlwrm2016-04-04 13:09:05
Ruby on Rails
mlwrm, 2016-04-04 13:09:05

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

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

When I try to log into /admin from under this user, it says You are not authorized to access this page.
How to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
N. Bekseitov, 2016-04-05
@malworm

Add config.authenticate_with in rails_admin.rb file

C
caution, 2016-04-05
@caution

maybe you have a redirect from the /admin page that is not authorized to a page for which there is no access to
any mb device.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question