嗨,我需要帮助与新的宝石'cancan‘(为我)
我还有一个问题:在我的应用程序中,我有'Post‘模型和'Photo’模型(路由:)
resources :posts do
resources :photos
end在ability.rb中,我写道:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.roles.first.nil?
can :read, Post
#not logged
elsif user.roles.first.name == "user"
#loged in as user
can :read, Post
can :create, Post
can :update, Post, :user_id => user.id
can :destroy, Post , :user_id => user.id
elsif user.roles.first.name == "admin"
# login as admin
can :manage, Post
end
end
end我不知道该怎么说:如果帖子是由另一个用户创建的,那么当前用户就没有访问页面的权限
localhost:3000/post/97/photos并且他(当前用户)不能在那里创建或销毁任何东西,换句话说,他只能读取localhost:3000/post/97/
但如果当前用户是作者-他有权访问localhost:3000/post/97/photos、localhost:3000/post/97/photos/new和localhost:3000/post/97/photo/244/show ...
在能力上类似于: can :destroy,Photo,@photo.post.user_id => user.id //但是如何定义@photo ??或者如果你知道一个更简单的方法?
发布于 2012-09-21 21:44:00
您可以使用如下所示的块:
can :manage , Post do | post |
post.user_id == user.id
end这意味着只有当前用户创建了帖子,才能对其进行管理。
https://stackoverflow.com/questions/12530072
复制相似问题