2011-04-07 59 views
2

因此,我使用CanCan的Rails 3進行授權。我希望所有用戶能夠刪除他們創建的照片,我想我有這個設置正確,但它是不工作...Rails + CanCan:用戶應該可以刪除自己的照片,但不能刪除

這裏是我的能力類:

class Ability 
    include CanCan::Ability 

    def initialize(user) 

    # ONLY REGISTERED USERS CAN DO THESE THINGS 
    unless user.nil? 

     # ALL REGISTERED USERS CAN DO THESE THINGS 
     can :read, [Photo, Context, Focus, LandUse, Vote, Rating, Comment, Profile, Article] 
     can :show, Page 

     # MEMBERS 
     if user.has_role? :member 
     can [:create, :read], Photo 
     can [:update, :destroy], Photo, :user_id => user.id 
     can :create, [Vote, Rating, Comment, Profile] 
     can :update, [Vote, Rating, Comment, Profile], :user_id => user.id 

     can [:show, :update], User do |u| 
      u == user 
     end 
     end 

     # CURATORS 
     ... 

     # ADMINS 
     ... 

    end 

    # ALL VISITORS CAN DO THESE THINGS 
    can :create, [User, Profile] 
    can :request_invite, User 
    # can :show, Page 

    end 
end 

這裏是我的控制器操作:

def destroy 
    @photo = Photo.find(params[:id]) 

    authorize! :delete, @photo 
    @photo.destroy 
    redirect_to :back, :notice => 'Photo deleted.' 
end 

任何想法這裏有什麼問題?

回答

5

應該是authorize! :destroy, @photo您的控制器的destroy操作。

+0

看起來你在我之前已經得到了答案...我投票刪除我的答案。 – 2011-04-07 15:24:56

+0

* doh。*謝謝。 – Andrew 2011-04-07 15:26:03

1

其實,如果你在你的控制器使用load_and_authorize_resource,你可以離開了

@photo = Photo.find(params[:id]) 
authorize! :destroy, @photo 

因爲它已經被慘慘完成。