【发布时间】:2016-06-08 14:44:49
【问题描述】:
我正在使用 Cancancan 设置管理员权限。我有一个关联的房间
has_one :group, through: :property
我知道无法通过使用 cancancan 传递对象,但我们可以使用哈希来检查能力。
#ability.rb
if user.has_role?(:admin)
can :manage, Room, :group => { :id => user.group_id }
end
上述能力检查在我加载我的索引、显示、更新、编辑和销毁方法时有效,但在我加载新方法或创建方法时无效。 我知道 Cancancan 将任何条件作为散列传递,例如 this,但我认为它不会传递我的散列,因为组被设置为关联而不是每个房间对象上的列。
#rooms_controller.rb
class RoomsController < ApplicationController
load_and_authorize_resource
def new
@room = Room.new
end
end
我的问题是,我怎样才能在 new 和 create 方法上检查这种能力,最好不要向我的对象添加新列。
感谢任何帮助
谢谢
【问题讨论】:
-
什么时候设置房间属于哪个组?
-
它通过创建时的属性进行设置。每组大约有6个房产,每个房产大约有10个房间。
标签: ruby-on-rails cancan user-roles cancancan