2012-04-27 2 views
0

응답을 Rails로 읽음으로 표시하는 버튼을 추가하려고합니다. 나는 현재 이와 같은 것을 가지고있다.Cancan 허용 Ability.rb 모델의 특정 필드에만 액세스 관리

# /app/models/ability.rb 
... 
can :manage, Reply, :user_id => user.id 
... 

은 내가 읽을 때 사용자가 회신을 표시 할 수있는 버튼이 내 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 

load_and_authorize_resource 있습니다.

= 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

같은 컨트롤러에 대한 새로운 액션을 정의 할 수 있습니다 원하는 무엇으로 모두

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

을 가질 수 있다고 생각

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

0

나는 업데이트 작업 만 표시 답글입니다 경우 다음 읽을 그건 당신이

관련 문제