[英]How do I restrict a role from not seeing prices on Products?
我有一個Product
模型,並且如果用戶以guest
角色登錄或未登錄,我不希望他們能夠在我的應用程序中查看產品的價格。
我正在使用Devise,CanCan和Rolify。
我在我的ability.rb
嘗試了這個:
user ||= User.new # guest user (not logged in)
if user.has_role? :guest
can :read, [Product, Vendor, Banner]
cannot :read, [Product.price]
end
但這似乎不起作用。 我沒有在視圖中添加任何代碼-我需要這樣做,還是該ability
課程根本不顯示價格?
我正在收回我所說的話。 您可以使用CanCan以某種方式執行此操作,但這似乎有些困難。 您可以嘗試以下嗎?
if user.has_role? :guest
can :read, [Product, Vendor, Banner]
cannot :view_prices, Product
end
然后,在您的視圖中,您必須手動檢查用戶是否可以查看價格
<% if can? :view_prices, Product %>
<%= product.price %>
<% end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.