Ruby on rails 视图中的cancancan auth

Ruby on rails 视图中的cancancan auth,ruby-on-rails,cancan,cancancan,Ruby On Rails,Cancan,Cancancan,我正在将cancancan添加到我的应用程序中以获得授权,我可以跨越一个障碍 我有一个投票系统,你不能在自己的条目上投票,但你可以编辑、删除等 因此,我在能力类中创建了一个块 def initialize(user) if user.present? if user.admin? can :manage, :all else can :vote, Entry do |entry| user.id != entry.user_id

我正在将cancancan添加到我的应用程序中以获得授权,我可以跨越一个障碍

我有一个投票系统,你不能在自己的条目上投票,但你可以编辑、删除等

因此,我在能力类中创建了一个块

def initialize(user)
  if user.present?
    if user.admin?
      can :manage, :all

    else
      can :vote, Entry do |entry|
        user.id != entry.user_id
      end
      can :manage, Entry, user_id: user.id
      can :manage, Message, user_id: user.id
      can :manage, Profile, user_id: user.id
      can :manage, User, user_id: user.id

    end

   can :read, :all

  end
 end
这是我叫罐头的地方?方法:

<% @entries.each do |x| %>

 <% if can? :vote, x %>
  <span class="text-green"> <%= link_to like_entry_path(x), method: :put, remote: true do %>
    <i class="fas fa-chevron-up"></i>
  <% end %>
  </span>
  <span class="badge" id="upvote-count<%=x.id%>"><%= x.get_upvotes.size%></span>
  <span class="text-red"> <%= link_to unlike_entry_path(x), method: :put, remote: true do %>
    <i class="fas fa-chevron-down"></i>
  <% end %>
  </span>
  <span class="badge" id="downvote-count<%=x.id%>"><%= x.get_downvotes.size%></span>
 <% else %>
  <i class="fas fa-chevron-up text-grey"></i>
  <span class="badge"><%= x.get_upvotes.size%></span>
  <i class="fas fa-chevron-down text-grey"></i>
  <span class="badge"><%= x.get_downvotes.size%></span>
 <% end %>
<% end %>

但它仍然在打印链接,而不应该打印

调试时,我在第一个条目上选中
user.id!=entry.user_id
,其返回值为false,因此我可以对其进行投票,但在第二个循环中,相同的查询返回为true,但链接仍然显示在视图中


总而言之,如果这有点让人困惑的话,那么两个条目被输入到两个不同的帐户,但投票链接出现在两个条目上。

定义如下能力:

cannot :vote, Entry if  user_id: user.id 

Doc:

如果您在能力文件中交换
can:manage,Entry
can:vote,Entry
,该怎么办?您是否首先在数据库中检查了您的用户和输入用户id是否正确?@srasher是的,我检查了数据库,@A阅读没有什么区别不幸的是你的用户管理员吗?不,这是因为投票链接在不应该显示的时候显示,所以cancan功能出于某种原因不起作用。你知道我认为是什么吗?我只是想在网站上运行更多的测试!,你救了我,我简直是在胡思乱想,要永远卸载坎坎坎!