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 : 코멘트 =>를 통해 문서 : =>를 통해 다른 사람을 읽을 수있는 사용자에게 원하는 경우 사용자는

사용자 모델에서 몇 관계를 추가 한 후 코멘트

0

InheritedRe user.comment_ids commentable_id => : 읽기, 코멘트 :

는 그런 능력 클래스에서

수를 추가 소스는 PARAM belongs_to 추가해야합니다

belongs_to :project, :optional => true 

하면 자세한 내용을 알아 @https://github.com/ryanb/cancan/wiki/Inherited-Resources