3

I am using RoR, CanCanCan, Rolify, and ActiveAdmin. I can log in and see pages, but cannot login as admin. I am not sure what I am doing wrong here. admin/dashboard.rb

ActiveAdmin.register_page "Dashboard" do

  menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }

  content title: proc{ I18n.t("active_admin.dashboard") } do
  div class: "blank_slate_container", id: "dashboard_default_message" do
      span class: "blank_slate" do
        span I18n.t("active_admin.dashboard_welcome.welcome")
        small I18n.t("active_admin.dashboard_welcome.call_to_action")
      end
    end
  end
end

controllers/application_controller.rb

class ApplicationController < ActionController::Base
 protect_from_forgery with: :exception

  def authenticate_active_admin_user!
     authenticate_user!
   unless current_user.role?(:admin)
      flash[:alert] = "You are not authorized to access this resource!"
      redirect_to root_path
   end
    end

end

models/ability.rb

class Ability
  include CanCan::Ability

  if user.has_role? :admin
   can :manage, :all
  else
    can :read, :all
  end

  def initialize(user)

  end
  can :manage, :all if user.is? :admin

end

user.rb class User < ActiveRecord::Base rolify

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
    def role?(role)
      return !!self.roles.find_by_name(role.to_s.camelize)
  end
end

config/initializer/active_admin.rb

ActiveAdmin.setup do |config|
config.authentication_method = :authenticate_active_admin_user!
config.current_user_method = :current_user
config.logout_link_path = :destroy_user_session_path
config.logout_link_method    = :delete
config.batch_actions = true
config.localize_format = :long
end

0 Answers0