4

我已经尝试了所有可能的方法,但似乎无法使用 cancan 限制 active_admin 中的 scope_to 方法。

我可以让菜单和大多数其他方法一样玩得很好,但不是 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 方法由 devise 和 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 方法吗?它适用于我需要它的其他任何地方。

4

1 回答 1

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
于 2011-11-09T10:22:15.613 回答