0

我正在尝试添加一个按钮以在 Rails 中将回复标记为已读。我目前有这样的东西。

# /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

我需要一种方法,只允许用户管理isRead?他的 user.id 匹配的对象的属性to_user_id

4

2 回答 2

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

顺序非常重要。现在登录的用户可以管理回复,并且作为用户的用户将只能标记_as_read。

于 2012-04-27T00:49:08.087 回答
0

我想你可以两者兼得

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

如果更新操作仅用于将回复标记为已读,那么这就是您想要的

于 2012-04-27T00:45:38.807 回答