gpt4 book ai didi

ruby-on-rails - Rails + CanCan : User should be able to delete own photo, 但不能

转载 作者:行者123 更新时间:2023-12-04 05:37:43 25 4
gpt4 key购买 nike

所以,我使用 Rails 3 和 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

这是我的 Controller 操作:

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

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

知道这里出了什么问题吗?

最佳答案

应该是授权! :destroy, @photo 在 Controller 的 destroy 操作上。

关于ruby-on-rails - Rails + CanCan : User should be able to delete own photo, 但不能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5583307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com