简体   繁体   English

CanCanCan 仅在具有属性时才能查看

[英]CanCanCan view only if has attribute

Im using CanCanCan to manage my authorizations pages.我使用 CanCanCan 来管理我的授权页面。

This is how is set my Abilities page:这是设置我的能力页面的方式:

class Ability
 include CanCan::Ability
 def initialize(user)

#return if user.nil?
if user.nil? 
  can :read, User
  can :read, Talent, {is_major: false}
else

A User has_one Talent.一个用户拥有一个天赋。 And a Talent has a method called: is_major.一个 Talent 有一个方法叫做:is_major。 This method checks if the talent have more then 18 years old.此方法检查人才是否超过 18 岁。

I want that, a User that is not logged in on the app, can only read a Talent if this Talent is_major.我希望,未登录应用程序的用户只能在此人才是_专业的情况下阅读人才。

How can I setup it on CanCanCan?如何在 CanCanCan 上设置它?

The problem here was only a sintax:这里的问题只是一个sintax:

If I add:如果我添加:

if user.nil? 
  can :read, User
  can :read, Talent, is_major?: true
else

It works.有用。

You'll want to define the ability with block syntax since is_major?is_major?开始,您需要使用块语法定义能力 is a model method.是一种模型方法。

can :read, Talent do |talent|
    talent.is_major?
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM