0

有機型用戶,項目文檔,問題,評論,能力:授權嵌套和多態資源(康康舞)

class User < ActiveRecord::Base 
    has_and_belongs_to_many :projects 
end 

class Project < ActiveRecord::Base 
    has_and_belongs_to_many :users 
    has_many :documents 
end 

class Issues < ActiveRecord::Base 
    belongs_to :project 
    has_many :comments, :as => :commentable 
end 

class Document < ActiveRecord::Base 
    belongs_to :project 
    has_many :comments, :as => :commentable 
end 

class Comment < ActiveRecord::Base 
    belongs_to :commentable, :polymorphic => true 
end 

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.current 

    can :read, Project, :id => user.project_ids 
    can :manage, Document, :project => { :id => user.project_ids } 
    end 
end 

路線:

resources :projects, :shallow => true, :path => '/', :only => :show do 
    resources :documents do 
    resources :comments 
    end 
    resources :issues do 
    resources :comments 
    end 
end 

控制器:

class DocumentsController < InheritedResources::Base 
    load_and_authorize_resource :project 
    load_and_authorize_resource :document, :through => :project, :shallow => true 

    # GET /1/documents 
    # GET /1/documents.json 
    def index 
    @project = Project.find(params[:project_id]) 
    @documents = @project.documents.page(params[:page]) 

    respond_to do |format| 
     format.html 
     format.json { render :json => @documents } 
    end 
    end 

    # POST /1/documents/new 
    # POST /1/documents/new.json 
    def new 
    @project = Project.find(params[:project_id]) 
    @document = @project.documents.build 

    respond_to do |format| 
     format.html 
     format.json { render :json => @document } 
    end 
    end 

    # POST /1/documents 
    # POST /1/documents.json 
    def create 
    @project = Project.find(params[:project_id]) 
    @document = @project.documents.build(params[:document]) 

    respond_to do |format| 
     if @document.save 
     format.html { redirect_to @document, notice: 'Document was successfully created.' } 
     format.json { render json: @document, status: :created, location: @document } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @document.errors, status: :unprocessable_entity } 
     end 
    end 
    end 
end 

項目能力可以,但創建文檔失敗。 問題:

  1. 我對文檔能力做錯了什麼?
  2. 如何編寫評論(多態和嵌套)能力?

回答

0

爲了創建文檔

加:能力班「可以創建,文件」,因爲在當時創造動作被稱爲「項目」是針對文檔爲零。

寫作能力評論:

你能解釋一下你想達到什麼嗎?如果你想用戶讀取其他用戶的評論則

添加用戶模型幾個關係:

的has_many:文件:通過=>:項目

的has_many:評論:通過=>:文件

然後在你的能力類添加

可以:閱讀,評論:commentable_id => user.comment_ids