如何使用CanCanCan与枚举场? [英] How to use CanCanCan with enum field?

查看:355
本文介绍了如何使用CanCanCan与枚举场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文章模型枚举字段枚举状态:[:悬而未决,:做]。

下面是我的能力,文件

 类能力
  包括惨惨::能力  高清初始化(用户)
    用户|| = User.new    如果user.member?
      可以:阅读,Article.done
    结束
  结束
结束

在视图中我试图呈现的 Article.done 收集成员,但没有任何事情渲染。

 <若能%? :读,Article.done%GT;
  <%=渲染部分:'文章',收藏:Article.done,如:本文%GT;
<%结束%GT;

所以我有一个问题:有没有可能在CanCanCan与枚举的工作方式


解决方案

我可能是错的,,但我认为枚举仅创建实例方法:

  @article = Article.find PARAMS [:ID]
@ article.done?

我的的错误 ...


  基于枚举字段的允许值

斯科普斯将提供
  为好。与上面的例子:

  Conversation.active
Conversation.archived


-

我会删除此,如果它不能正常工作;我会评估对环境的散,不类本身:

 类能力
  包括惨惨::能力  高清初始化(用户)
    用户|| = User.new    如果user.member?
      可以:阅读,文章,状态:Article.statuses [:做]
    结束
  结束
结束


更新

要考虑几个重要的事情。

首先,使用条件时:


  

重要信息:如果存在一个块或条件的哈希值的一类检查时,他们会被忽略,并且将返回true。


这意味着你不能调用一个能力,而路过的一类,它在一个对象的实例被称为特定条件。

其次,似乎 CanCanCan有一些问题,评估枚举,这使得需要以下code:

 #应用程序/模型/ ability.rb
一流的能力
  包括惨惨::能力  高清初始化(用户)
    用户|| = User.new
    如果user.member?
      能:读,做条|文章|
        article.done?
      结束
    结束
  结束
结束

然后,您需要到文章实例传递给方法:

 #应用程序/视图/用品/ index.html.erb
<表>
    &所述;%=渲染@articles%GT;
< /表>#应用程序/视图/条/ _article.html.erb
<%如果能? :阅读,文章%GT;
    &所述; TR>
      &所述; TD>标题:其中; / TD>
      &所述; TD>&下;%= article.title%GT;&下; / TD>
      &所述; TD>&下;%= article.status%GT;&下; / TD>
    < / TR>
<%结束%GT;

I got Article model with enum field enum status: [:pending, :done].

Here's my ability file

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    if user.member?
      can :read, Article.done
    end
  end
end

In view I am trying to render Article.done collection for member but nothings renders.

<% if can? :read, Article.done %> 
  <%= render partial: 'article', collection: Article.done, as: :article %>
<% end %>

Therefore I have a question: is there any possible way to work with enum in CanCanCan?

解决方案

I may be wrong, but I think that enum only creates instance methods:.

@article = Article.find params[:id]
@article.done?

I was wrong...

Scopes based on the allowed values of the enum field will be provided as well. With the above example:

Conversation.active
Conversation.archived

--

I'll delete this if it doesn't work; I would be evaluating against the hash of conditions, not the class itself:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new

    if user.member?
      can :read, Article, status: Article.statuses[:done]
    end
  end
end


Update

Several important things to consider.

Firstly, when using hash conditions:

Important: If a block or hash of conditions exist they will be ignored when checking on a class, and it will return true.

This means that you cannot call specific conditions on an "ability" whilst passing a class, it has to be called on an instance of an object.

Secondly, it seems CanCanCan has some issues evaluating enum values, which makes the following code necessary:

#app/models/ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.member?
      can :read, Article do |article|
        article.done?
      end
    end
  end
end

You then need to pass the article instance to the can? method:

#app/views/articles/index.html.erb
<table>
    <%= render @articles %>
</table>

#app/views/articles/_article.html.erb
<% if can? :read, article %>
    <tr>
      <td>Title:</td>
      <td><%= article.title %></td>
      <td><%= article.status %></td>
    </tr>
<% end %>

这篇关于如何使用CanCanCan与枚举场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆