不使用Rails中的Grape API gem来理解嵌套资源

时间:2014-07-17 18:27:57

标签: ruby-on-rails grape grape-api

以下是我的API的样子:

module ServiceRequests
  class API < Grape::API
    version 'v1', using: :path
    format :json

    namespace :companies do
      params do
        requires :company_id, type: Integer, desc: "A Company Id"
      end

      resource :service_requests do
        get :all do
          Company.find(params[:company_id]).service_requests
        end
      end
    end

  end
end

我正在尝试拨打这样的电话:http://localhost/v1/companies/:company_id/service_requests/all但是当我拨打此电话时,我收到错误No route matches [GET] "/v1/companies/1/service_requests/all"

工作代码:

module ServiceRequests
  class API < Grape::API
    version 'v1', using: :path
    format :json

    resource :companies do

      params do
        requires :company_id, type: Integer, desc: "A Company Id"
      end

      route_param :company_id do
        resource :service_requests do
          get do
            Company.find(params[:company_id]).service_requests
          end
        end
      end

    end

  end
end

1 个答案:

答案 0 :(得分:5)

你试过这个吗?

namespace 'companies/:company_id' do
  resource :service_requests do
    get :all do
      Company.find(params[:company_id]).service_requests
    end
  end
end