2012-04-27 60 views
0

我试图添加一个按钮来标记在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?

回答

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功能。

+0

我会建议为mark_as_read添加不同的操作,而不是授予更新操作的权限。这样Update操作可以用于修改回复 – naren 2012-04-27 00:52:35

0

我认为你可以同时拥有

can :manage, Reply, :user_id => user.id 
can :update, Reply, :to_user_id => user.id 

如果更新操作只对标记回复为,然后读这就是你想要什么

相关问题