如何修改cancan load并授权资源使用不同的id加载资源

Bra*_*kti 4 ruby-on-rails cancan ruby-on-rails-3 ruby-on-rails-4 cancancan

如何修改加载并授权资源使用不同的id加载资源。对于前。

我的路线是

http://localhost:3000/organization/user/12/event/20/edit
Run Code Online (Sandbox Code Playgroud)

在我的事件控制器中,我正在使用:event_id和用户使用访问事件:id

现在在我的事件控制器中如何修改load_and_authorize_resource使用:event_id而不是:id加载事件

kar*_*gen 5

如果资源尚未加载到实例变量中,则只会将其加载到实例变量中。这使您可以轻松地覆盖在单独的 before_filter 中加载的方式。

class EventsController < ApplicationController
  before_filter :find_event
  load_and_authorize_resource

  private

  def find_event
    @event = Event.find(params[:event_id])
  end
end
Run Code Online (Sandbox Code Playgroud)

在文档中阅读更多内容:https://github.com/CanCanCommunity/cancancan/wiki/Authorizing-controller-actions