2012-03-29 2 views
4

에 제한 권한 :2.0 캉캉 - 나는 나의 능력 모델에서 다음이 필드

class Ability 
    include CanCan::Ability 

    #... 

    def superuser_rules 
    can :access, :items  
    cannot :update, :items 
    can :update, :items, :foo_attributes 
    end 

end 

나는 것을 반영 양식을 가지고에만 foo_attributes 양식을 중첩 표시하여.

그러나 양식을 제출할 때 항목 업데이트가 거부되었습니다.

새로운 경로/작업을 추가하지 않고이를 피할 수있는 방법이 있습니까?

감사합니다.

답변

1

이러한 "특수 속성"을 처리하기위한 새 작업을 만들 수 있습니다.

먼저 params의 특수 특성을 정리할 수 있습니다.

class UserController 
    before_filter :only => [:create, :update] { params[:user].delete(:accepted_at) } 
end 

그런 다음 당신은 특별한 속성을 변경하기 위해 특별한 조치를 만들 :

def accept 
    User.find(params[:user_id]).update_attributes :accepted_at => Time.now 
end 

이제 create, updateaccept 행동에 대해 서로 다른 권한을 설정할 수 있습니다.

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    if user && user.admin? 
     can :accept, User 
    elsif user 
     can :update, User 
    end 
    can :create, User 
    end 
end 

Take a look at this too