在Rails中对子模型使用Cancan授权

时间:2014-09-06 17:32:30

标签: ruby-on-rails ruby-on-rails-4

我正在使用CanCanCan,我试图禁止用户访问其他用户的图库,但无法找到在Ability类中执行此操作的正确方法。

用户has_one个人资料

个人资料has_one图库

  def user_abilities(user)
    can :read, Profile

    can [:edit, :update], Profile, user_id: user.id
    can [:edit, :update], Gallery, user_id: user.id # also tried gallery: { user_id: user.id }, profile_id: user.profile.id and so on

  end

在我看来,我已经(没有成功)尝试了我在StackOverflow上可以找到的所有建议。

1 个答案:

答案 0 :(得分:0)

在仔细阅读文档后,我自己解决了这个问题

class GalleriesController < ApplicationController

  load_and_authorize_resource :profile
  load_and_authorize_resource :gallery, through: :profile, singleton: true