【问题标题】:CanCan gem with nested resources and :find_by具有嵌套资源和 :find_by 的 CanCan gem
【发布时间】:2011-10-17 02:20:52
【问题描述】:

我使用 CanCan load_and_authorize_resource 辅助方法来获取资源和生成授权,但我有一个这样的嵌套资源

load_and_authorize_resource :company
load_and_authorize_resource :accountings, :through => :company, :class => 'Departments::Accounting'

但我需要 @accountings 由另一个属性而不是 Departmets::Accounting id 找到,并为该属性赋值,例如

@accountings = @company.find_by_period_id(@period.id)

【问题讨论】:

    标签: ruby-on-rails-3.1 cancan


    【解决方案1】:

    您可以通过两个额外的选项来做到这一点:

    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
    

    检查load_and_authorize_resourcehere的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多