2012-01-31 53 views
1

我在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 
    ... 
+0

你可以發佈'current_admin_user'定義的代碼嗎?我想你有一個session_helper或執行此操作的東西。 'AbstractController :: ActionNotFound'錯誤也可能連接到設計(使用Google搜索它顯示負載此錯誤)。 – HectorMalot 2012-02-08 13:10:14

回答

0

current_admin_user是紅寶石類的對象。

你能張貼類的內容(包含role方法?

我認爲,這個對象是不正確初始化。

你正在做一些錯用的if-else檢查。檢查此

+0

謝謝!添加更多詳細信息 – leonel 2012-01-31 00:36:11

+0

我在我的問題中添加了application_controller.rb文件。不應該先查看application_controller.rb然後invoices.rb?如果是這種情況,那麼在application_controller.rb中創建一個用戶,即使它是nil @current_ability || = Ability.new(current_admin_user)'那麼它是如何得到一個NilClass錯誤? 'nil的未定義方法角色:NilClass' – leonel 2012-01-31 15:30:32

+0

嘿,我已經發布了包含角色方法的類的內容,它是AdminUser類。請有人幫忙。 – leonel 2012-02-08 05:41:50

0

看起來你需要通過設置你當前的管理用戶變量來使當前用戶對象可用於視圖當前current_admin_user是零,所以它顯然沒有被定義在你的會話中嘗試類似以下內容控制器,如果你有。

def get_user 
    current_admin_user = session[:current_user] 
end 
+0

ActiveAdmin正在使用Devise和CanCan處理它。我沒有會議控制器 – leonel 2012-02-09 20:16:52

0

action_methods預期的結果是動作名稱的數組,所以當你嘗試從該方法返回一個重定向,你應該期待一個例外。您應該確保您擁有使用過濾器前登錄的用戶(例如before_filter :authenticate_user!)。

另一件事我會檢查(因爲我沒有使用ActiveAdmin或一般的抽象控制器)爲確保您的一個AbstractController(controller do ... end)先後獲得了設計控制器方法 - 否則load_and_authorize_resourceauthenticate_user!等將失敗。