簡體   English   中英

Rails Cancan寶石

[英]Rails cancan gem

嗨,我需要新的(對我來說)寶石'cancan'的幫助

我還有一個問題:在我的應用中,我有“發布”模型和“照片”模型(路線:)

  resources :posts do
    resources :photos
  end

而在能力.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/

但是如果當前用戶是autor-他可以訪問localhost:3000/post/97/photoslocalhost:3000/post/97/photos/newlocalhost:3000/post/97/photo/244/show ...

能力是這樣的:can:destroy,Photo,@ photo.post.user_id => user.id //但是如何定義@photo? 或者,如果您知道更簡單的方法?

您可以使用如下代碼:

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

這意味着只有當前用戶創建了帖子,它才可以管理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM