我在ActiveAdmin中有這個自定義控制器,允許根據用戶角色顯示按鈕。我這樣做是在應用程序/管理/ invoices.rb文件Rails 3 ActiveAdmin。如果沒有登錄,重定向
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.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
的能力類ability.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
/app/admin/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
...
你可以發佈'current_admin_user'定義的代碼嗎?我想你有一個session_helper或執行此操作的東西。 'AbstractController :: ActionNotFound'錯誤也可能連接到設計(使用Google搜索它顯示負載此錯誤)。 – HectorMalot 2012-02-08 13:10:14