Ruby on rails 如何使用rails创建API方法&;用于嵌套资源的招摇过市文档
我有一个rails应用程序,使用活动记录序列化程序响应json或html。我用它来创建一个公共API。我使用Desive simple http进行基本身份验证 我正在通过以下gems使用Swagger文档Ruby on rails 如何使用rails创建API方法&;用于嵌套资源的招摇过市文档,ruby-on-rails,json,api,resources,swagger,Ruby On Rails,Json,Api,Resources,Swagger,我有一个rails应用程序,使用活动记录序列化程序响应json或html。我用它来创建一个公共API。我使用Desive simple http进行基本身份验证 我正在通过以下gems使用Swagger文档 gem'swagger docs'#用于创建swagger json格式 gem'swagger-ui#rails'#用于生成时髦的活动文档ui 我已经能够通过控制器成功地为我的主要顶级资源创建json。例如,在“products_controller.rb”中: 问题是如何设置嵌套资源
gem'swagger docs'#用于创建swagger json格式
gem'swagger-ui#rails'#用于生成时髦的活动文档ui
我已经能够通过控制器成功地为我的主要顶级资源创建json。例如,在“products_controller.rb”中:
问题是如何设置嵌套资源
所以,在我的体系结构中,产品有许多插槽/插槽属于产品。因此,在我的“slots\u controller.rb”中,我以相同的方式设置它:
swagger_controller :slots, "Slot Management"
swagger_api :index do
summary "Fetches all Slots for a Product"
param :query, :page, :integer, :optional, "Page number"
param :form, :product_id, :integer, :required, "Product id"
response :unauthorized
response :success
end
我认为这是一厢情愿的想法,即提供:product_id
的参数以及使用产品id查找给定产品插槽的控制器操作,然后swagger可能会自动神奇地将其解释为嵌套资源。似乎不是,相反,我在Swagger中的slots API方法正在寻找:
/api/v1/slots.json
而不是
/api/v1/products/#{product.id}/slots
如何设置我的swagger\u控制器来为插槽生成正确的嵌套url结构?不确定是否已解决此问题,但您可以使用
:path
参数类型,如下所示:
swagger_api :index do
summary "Fetches all Slots for a Product"
param :path, :product_id, :integer, :required, "Product id"
param :query, :page, :integer, :optional, "Page number"
response :unauthorized
response :success
end
谢谢你,马克。实际上,我暂时放弃了swagger,而是用它来记录我的API。我可能会返回到swagger来提供活动文档,但目前使用Slate进行迭代要快得多。
swagger_api :index do
summary "Fetches all Slots for a Product"
param :path, :product_id, :integer, :required, "Product id"
param :query, :page, :integer, :optional, "Page number"
response :unauthorized
response :success
end