3

这是我的 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
4

1 回答 1

6

你试过这样做吗?

namespace 'companies/:company_id' do
  resource :service_requests do
    get :all do
      Company.find(params[:company_id]).service_requests
    end
  end
end
于 2014-07-17T18:41:22.413 回答