current_user和active_admin

时间:2011-10-07 13:28:01

标签: ruby-on-rails ruby-on-rails-3 devise cancan activeadmin

我已尽可能尝试使用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方法的?它适用于我需要它的任何其他地方。

1 个答案:

答案 0 :(得分: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
相关问题