[英]Allowing cancan Ability.rb manage access only specific fields of a model
我正在嘗試添加一個按鈕以將回復標記為在Rails中已讀。 我目前有這樣的事情。
# /app/models/ability.rb
...
can :manage, Reply, :user_id => user.id
...
我的load_and_authorize_resource
中也有load_and_authorize_resource
# /app/controllers/replies_controller.rb
class RepliesController < ApplicationController
load_and_authorize_resource
def update
@reply = Reply.find(params[:id])
@reply.isRead = true
if @reply.save
flash[:notice] = "Marked as ready."
flash[:alert] = params[:id]
redirect_to root_path
else
render :action => 'new'
end
end
我有一個按鈕,用戶可以將回復標記為已讀。
= button_to "Mark as read", idea_reply_path(reply.idea,reply), :method => "put"
問題是,由於我要嘗試根據能力.rb(上)中定義的其他user.id所有者更新對象,因此我沒有權限對其進行編輯。
如果我添加這樣的內容,它將起作用,但是我也將管理整個答復對象的權限授予其他人。
can :manage, Reply, :to_user_id => user.id
我需要一種只允許用戶管理isRead?
屬性的isRead?
他的user.id與to_user_id
相匹配的to_user_id
。
您可以在控制器中為mark_as_read定義一個新動作
def mark_as_read
#action to mark as read
end
並在能力中定義
can :manage, :Reply, :user_id => user.id
can :mark_as_read, :to_user_id => user.id
訂購非常重要。 現在,已登錄的用戶可以管理答復,並且作為該用戶的用戶將僅具有mark_as_read的能力。
我想你們可以兩者兼得
can :manage, Reply, :user_id => user.id
can :update, Reply, :to_user_id => user.id
如果更新操作僅用於將標記回復標記為已讀,那么這就是您想要的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.