我试图添加一个按钮来标记在Rails中读取的答复。我目前有这样的事情。允许cancan Ability.rb只管理访问模型的特定字段
# /app/models/ability.rb
...
can :manage, Reply, :user_id => user.id
...
我也load_and_authorize_resource
在我RepliesController
# /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"
问题是,因为我想在ability.rb(顶部)定义更新来自其他user.id所有者的对象,我没有权限对其进行编辑。
如果我添加这样的东西它会工作,但我也赋予管理整个回复对象给其他人的权利。
can :manage, Reply, :to_user_id => user.id
我需要一种方法来只允许用户管理对象,他是user.id匹配to_user_id
的属性isRead?
。
我会建议为mark_as_read添加不同的操作,而不是授予更新操作的权限。这样Update操作可以用于修改回复 – naren 2012-04-27 00:52:35