我正在尝试添加一个按钮来将回复标记为在Rails中读取。我现在有类似的东西。
# /app/models/ability.rb
...
can :manage, Reply, :user_id => user.id
...
我的RepliesController
中也有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"
问题是,因为我正在尝试从capability.rb(顶部)中定义的其他user.id所有者更新对象,所以我没有编辑它的权限。
如果我添加这样的内容它会起作用,但我也有权管理整个回复对象给另一个人。
can :manage, Reply, :to_user_id => user.id
我需要一种方法只允许用户管理一个对象的属性isRead?
,其中user.id与to_user_id
匹配。
答案 0 :(得分:3)
您可以在控制器中定义新操作,例如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。
答案 1 :(得分:0)
我认为你可以同时拥有
can :manage, Reply, :user_id => user.id
can :update, Reply, :to_user_id => user.id
如果更新操作仅用于标记回复阅读,那么这就是你想要的