0

Hi i need help with new ( for me ) gem 'cancan'

i have a next problem: in my app i have 'Post' model and 'Photo' model ( routes: )

  resources :posts do
    resources :photos
  end

and in ability.rb i write:

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

and i dont know how put thats logic: if post created by another user, current user does not have access to page

localhost:3000/post/97/photos

and he(current user) can't create there anything or destroy, in other words he can only read localhost:3000/post/97/

But if current user is autor - he have access to localhost:3000/post/97/photos, localhost:3000/post/97/photos/new and localhost:3000/post/97/photo/244/show ...

in ability something like that: can :destroy, Photo , @photo.post.user_id => user.id // but how to define @photo ?? or if you know an easier way ?

4

1 に答える 1

1

次のようなブロックを使用できます。

can :manage , Post  do | post |
  post.user_id == user.id
end

つまり、現在のユーザーが投稿を作成した場合にのみ、それを管理できます。

于 2012-09-21T13:43:59.797 に答える