2011-10-07 74 views
3

我嘗試了各種可能的方式,似乎無法在使用cancan的active_admin中限制scope_to方法。current_user和active_admin

我能發揮好以及大多數其他方法,但是沒有scope_to

例如這工作正常菜單:

menu :if => proc{ can?(:manage, Booking) } 

但不是

scope_to :current_user, :association_method => :space_bookings unless proc{ can?(:manage, Booking) } 

scope_to :current_user, :association_method => :space_bookings unless proc{ current_user.admin? } 

scope_to :current_user, :association_method => :space_bookings unless controller.current_ability.can?(:manage, config.resource) 

如果我嘗試使用沒有proc的current_user方法,我得到一個未定義的錯誤。該CURRENT_USER方法是通過裝置和後也在active_admin.rb

# == Current User 
    # 
    # Active Admin will associate actions with the current 
    # user performing them. 
    # 
    # This setting changes the method which Active Admin calls 
    # to return the currently logged in user. 
    config.current_user_method = :current_user 

任何想法如何,我可以訪問到CURRENT_USER方法定義的?它適用於我需要的任何地方。

+0

似乎是一個懸而未決的問題:https://github.com/gregbell/active_admin/issues/70 – holden

回答

3

訪問方法,即使是active_admin中的application_controller中的方法通常也很痛苦。

但是,您可以通過明確重寫控制器來完全跳過它,但這並不理想,但它可以工作。

controller do 
    # authorize_resource 
    def index 
     if can? :manage, :all 
     @bookings = Booking.page params[:page] #you must call .page for the index to work. 
     else 
     @bookings = current_user.bookings.page params[:page] 
     end 
     index! #this is needed unless you are using custom views 
    end 
    end 
+0

我很新的紅寶石..什麼文件不上面的代碼進入? – baash05

+2

@ baash05它看起來像是用於ActiveAdmin頁面的控制器代碼,即。在ActiveAdmin.register do ... end'塊內 – rogerkk