[英]How to get a permission from a relationship using CanCanCan?
在我的應用程序中,我有一個名為User的has_one Talent模型。
我可以在CanCan中具備此功能:
class Ability
include CanCan::Ability
def initialize(user)
if user.nil?
can :read, User
can :read, Talent, is_public?: true
else
can :read, Talent, is_public?: true
end
我的頁面正在由ProfilesController#show呈現。 像這樣:
class ProfilesController < ApplicationController
before_action :check_ability, except: [:show]
def show
@user = User.find(params[:id])
authorize! :read, @user
authorize! :read, @user.talent
if current_user
sent_connections = current_user.sent_connections
connections = sent_connections + current_user.all_connections
@is_connected = !(connections.select { |c| c.user.id == @user.id }.empty?)
end
@top_5_photos = @user.top_5_photos
end
好。 我試圖渲染一個配置文件,方法:is_public返回false。 但是頁面顯示正確,而我期望的是由於以下原因用戶無法看到頁面:
can :read, Talent, is_public?: true
我在這里缺少什么?
如果我沒記錯的話
can :read, Talent, is_public?: true
^ is_public?
以上內容是Cancancan的屬性 。
但是因為is_public?
是自定義方法,那么您可以嘗試以下方法嗎?
can :read, Talent do |talent|
talent.is_public?
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.