以下是我的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
答案 0 :(得分:5)
你试过这个吗?
namespace 'companies/:company_id' do
resource :service_requests do
get :all do
Company.find(params[:company_id]).service_requests
end
end
end