如何获得漂亮的Grape API路由到嵌套集合

时间:2017-10-03 23:59:58

标签: ruby-on-rails grape-api

在下面的招摇图片中,列出project campaigns的我的端点正在返回预期的数据,但该网址包含不需要的细分。

如何构建代码以使嵌套集合或此类端点作为父模型的附件/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  

1 个答案:

答案 0 :(得分:0)

删除resource块并复制嵌套资源的参数可以正常工作,如下所示:

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