2017-10-03 10 views
0

아래 swagger 그림에서 projectcampaigns을 나열하는 내 끝점에서 원하는 데이터를 반환하지만 URL에 원치 않는 세그먼트가 있습니다.중첩 컬렉션에 예쁜 포도 API 경로를 얻는 방법

중첩 된 콜렉션 또는 이러한 엔드 포인트를 상위 모델의 ID : /api/v1/projects/{id}/campaigns에 부속으로 올리려면 코드를 어떻게 구성 할 수 있습니까? 다음

enter image description here

module API                                                   
    module V1                                                   
    class Projects < Grape::API                                              
     include API::V1::Defaults                                              

     resource :projects do                                               
     desc "Return all projects"                                             
     get "", root: :projects do                                             
      Project.all                                                
     end                                                   

     desc "Return a project"                                              
     params do                                                 
      requires :id, type: String, desc: "ID of the project"                                      
     end                                                   

     get ":id", root: "project" do                                            
      Project.where(id: permitted_params[:id]).first!                                       
     end                                                   

     resource :campaigns do                                              
      desc "Return all campaigns for a project"                                         
      get ":id/campaigns", root: "project" do                                         
      Project.find(params[:id]).campaigns                                          
      end                                                  
     end                                                   

     end                                                   
    end                                                    
    end                                                    
end 

답변

0

resource 블록 분리 및 중첩 자원의 중복 PARAMS 잘 작동 :

module API                                                   
    module V1                                                   
    class Projects < Grape::API                                              
     include API::V1::Defaults                                              

     resource :projects do                                               
     desc "Return all projects"                                             
     get "", root: :projects do                                             
      Project.all                                                
     end                                                   

     desc "Return a project"                                              
     params do                                                 
      requires :id, type: String, desc: "ID of the project"                                      
     end                                                   

     get ":id", root: "project" do                                            
      Project.where(id: permitted_params[:id]).first!                                       
     end                                                   

     desc "Return all campaigns for a project"                                         
     params do                                                 
      requires :id, type: String, desc: "ID of the project"                                      
     end                                                   

     get ":id/campaigns", root: "project" do                                          
      Project.find(params[:id]).campaigns                                          
     end                                                   

     end                                                   
    end                                                    
    end                                                    
end