在Rails中的子模型上使用Cancan授权



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

用户有一个配置文件(_O)

配置文件有一个库(_O)

  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上能找到的所有建议。

OK在仔细阅读文档后自行解决

class GalleriesController < ApplicationController
  load_and_authorize_resource :profile
  load_and_authorize_resource :gallery, through: :profile, singleton: true

最新更新