2017-10-03 15 views
0

下の写真では、projectcampaignsをリストする私のエンドポイントは意図したデータを返していますが、URLには不要なセグメントがあります。ネストされたコレクションにかなりのGrape 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 
関連する問題