gpt4 book ai didi

ruby-on-rails - 不理解在 Rails 中使用 Grape API gem 的嵌套资源

转载 作者:行者123 更新时间:2023-12-04 03:52:32 25 4
gpt4 key购买 nike

这是我的 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 但是当我进行此调用时,我收到了错误 没有路由匹配 [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

最佳答案

你试过这样做吗?

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

关于ruby-on-rails - 不理解在 Rails 中使用 Grape API gem 的嵌套资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com