我有一个Product
模型。
Ability
课程如下:
class Ability
include CanCan::Ability
def initialize(user)
if user.has_role? :super_admin
can :manage, :all
else
can :read, Product, Product.all.limit(10) if user.has_role? :brand_manager
can :access, :rails_admin # grant access to rails_admin
can :dashboard # grant access to the dashboard
end
end
end
以下是rails_admin
的代码:
RailsAdmin.config do |config|
config.authorize_with :cancan
end
我想隐藏列表中没有特定products
的所有name
?一些rails_admin
如何不支持它。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
您对hide all the producs from the list
的意思是什么?
如果您希望用户无法阅读,请尝试:
can :read, Product do |product|
[name1, name2].include?( product.name )
end
如果您不想退回这些产品,可以在控制器中使用:
Product.where( :name.in => [name1, name2] )
我希望我帮助
答案 1 :(得分:1)
你可以这样做。 1.在模型中创建一个新字段,如admin_user_id 2.在保存产品数据中的当前管理员用户详细信息时创建/更新产品 并使用它能力等级
class Ability
include CanCan::Ability
def initialize(user)
if user.has_role? :super_admin
can :manage, :all
elsif user.has_role? :brand_manager
can :manage,Product, :admin_user_id=> user.id
can :access, :rails_admin # grant access to rails_admin
can :dashboard # grant access to the dashboard
end
end
end