Ruby on rails 3.1 具有嵌套资源的CanCan gem和:通过

Ruby on rails 3.1 具有嵌套资源的CanCan gem和:通过,ruby-on-rails-3.1,cancan,Ruby On Rails 3.1,Cancan,我使用CanCanload\u和\u authorize\u resourcehelper方法获取资源并生成授权,但我有一个这样的嵌套资源 load_and_authorize_resource :company load_and_authorize_resource :accountings, :through => :company, :class => 'Departments::Accounting' 但我需要 @例如,accountings可以由另一个属性(而不是Depa

我使用CanCan
load\u和\u authorize\u resource
helper方法获取资源并生成授权,但我有一个这样的嵌套资源

load_and_authorize_resource :company
load_and_authorize_resource :accountings, :through => :company, :class => 'Departments::Accounting'
但我需要 @例如,accountings可以由另一个属性(而不是Departmets::Accounting id)找到,并为该属性提供一个值


@accountings=@company.find\u by\u period\u id(@period.id)

您可以通过两个额外选项来完成此操作:

load_and_authorize_resource :accountings,
    :through => :company, :class => 'Departments::Accounting',
    :find_by => :attr, # :period_id in your example
    :id_param => :something # this searches the params hash for
                            # params[:something], and sends that
                            # value to .find_by_attr
检查
加载和授权资源的代码