因此,我使用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
任何想法這裏有什麼問題?
看起來你在我之前已經得到了答案...我投票刪除我的答案。 – 2011-04-07 15:24:56
* doh。*謝謝。 – Andrew 2011-04-07 15:26:03