있는 모델 사용자, 프로젝트, 문서, 이슈, 코멘트, 능력 :권한 부여 중첩 및 다형성 자원 (캉캉)
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
프로젝트에 대한 능력은 괜찮지 만 문서 작성에 실패합니다. 질문 :
- 문서 기능이 잘못 되었나요?
- 주석 (다형성 및 중첩)에 대한 작성 기능은 어떻게 쓸 수 있습니까? 문서
추가를 만들기위한