【发布时间】:2018-06-07 15:35:35
【问题描述】:
我正在制作一个用于学习目的的系统。
该系统为用户控制医院轮班。用户应该只看到他的班次。
我正在使用 CanCanCan 进行授权控制。
所以对于索引页面,我有控制器操作:
class UsersController < ApplicationController
def index
@users = User.all
end
end
在 index.html.erb 中
<% @shifts.each do |shift| %>
<% if can? :read, shift %>
<tr>
<td><%= shift.date %></td>
</tr>
<% end %>
<% end %>
在我有能力方面:
class Ability
include CanCan::Ability
def initialize(user)
if user
can :manage, Shift, user_id: user.id
end
end
end
如果用户没有任何班次,我想向他显示一条消息。
但是在 index html 页面中使用 can 方法,因为如果列表不为空但没有 shift 当前用户可以看到我不能直接在 @shift 中使用空方法。
是否可以在用户控制器的索引操作中过滤列表?
【问题讨论】:
标签: ruby-on-rails ruby cancancan