У меня есть этот пользовательский контроллер в ActiveAdmin, позволяющий отображать кнопки в соответствии с ролями пользователя. Я делаю это в файле app / admin / invoices.rb
controller do
load_and_authorize_resource :except => :index
def scoped_collection
end_of_association_chain.accessible_by(current_ability)
end
def action_methods
['index'] + (current_admin_user.role=="administrator" ? ['edit','update','new','create','destroy', 'show'] : ['show'])
end
end
Если пользователь не вошел в систему, я получаю эту ошибку ...
NoMethodError in Admin::InvoicesController#index
undefined method `role' for nil:NilClass
Как я могу вместо этого перенаправить на страницу входа admin_root_path? Я также проверил что-то вроде этого ...
def action_methods
if current_admin_user.nil?
redirect_to admin_root_path
elsif current_admin_user.role == "administrator"
['index', 'edit','update','new','create','destroy', 'show']
elsif current_admin_user.role == "customer"
['index']
else
end
end
и я получаю эту ошибку
AbstractController::ActionNotFound (AbstractController::ActionNotFound):
Класс AdminUser adminuser.rb класса
class AdminUser < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me,
:customer_id, :role
validates :customer_id, :presence => true, :if => :is_customer?
belongs_to :customer
after_create { |admin| admin.send_reset_password_instructions }
def password_required?
new_record? ? false : super
end
def is_customer?
self.role == 'customer'
end
before_create :set_new_user_as_customer
def set_new_user_as_customer
self.role = 'customer'
end
end
Способность класса Способности. Rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= AdminUser.new
if user.role == "administrator"
can :manage, :all
elsif user.role == "customer"
cannot :create, :all
cannot :update, :all
cannot :destroy, :all
can :read, Shipment, :customer_id => user.customer_id
can :index, Invoice, :customer_id => user.customer_id
else
cannot :manage, :all
end
end
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
# Override build_footer method in ActiveAdmin::Views::Pages
require 'active_admin_views_pages_base.rb'
rescue_from CanCan::AccessDenied do |exception|
redirect_to admin_custom_dashboards_path, :alert => exception.message
end
def after_sign_in_path_for(resource_or_scope)
admin_custom_dashboards_path
end
def current_ability
@current_ability ||= Ability.new(current_admin_user)
end
end
/ приложение / администратор / invoices.rb
ActiveAdmin.register Invoice do
menu :if => proc{ can?(:manage, Invoice) }, :priority => 2
controller do
load_and_authorize_resource :except => :index
def scoped_collection
end_of_association_chain.accessible_by(current_ability)
end
def action_methods
['index'] + (current_admin_user.role=="administrator" ? ['edit','update','new','create','destroy', 'show'] : ['show'])
end
end
...