在下面的招摇图片中,列出project
campaigns
的我的端点正在返回预期的数据,但该网址包含不需要的细分。
如何构建代码以使嵌套集合或此类端点作为父模型的附件/api/v1/projects/{id}/campaigns
?
module API
module V1
class Projects < Grape::API
include API::V1::Defaults
resource :projects do
desc "Return all projects"
get "", root: :projects do
Project.all
end
desc "Return a project"
params do
requires :id, type: String, desc: "ID of the project"
end
get ":id", root: "project" do
Project.where(id: permitted_params[:id]).first!
end
resource :campaigns do
desc "Return all campaigns for a project"
get ":id/campaigns", root: "project" do
Project.find(params[:id]).campaigns
end
end
end
end
end
end
答案 0 :(得分:0)
删除resource
块并复制嵌套资源的参数可以正常工作,如下所示:
module API
module V1
class Projects < Grape::API
include API::V1::Defaults
resource :projects do
desc "Return all projects"
get "", root: :projects do
Project.all
end
desc "Return a project"
params do
requires :id, type: String, desc: "ID of the project"
end
get ":id", root: "project" do
Project.where(id: permitted_params[:id]).first!
end
desc "Return all campaigns for a project"
params do
requires :id, type: String, desc: "ID of the project"
end
get ":id/campaigns", root: "project" do
Project.find(params[:id]).campaigns
end
end
end
end
end