无法理解 Grape API 路由参数
Can't understand Grape API route param
我很难理解 Grape API,特别是 route_param
以及它如何与 params
一起工作。
考虑这段代码:
desc "Return a status."
params do
requires :id, type: Integer, desc: "Status id."
end
route_param :id do
get do
Status.find(param[:id])
end
end
这个街区生产什么路线?我知道这是一个 get
请求,但为什么它包含在 route_param
块中?为什么它不能在 params
块中?
您的街区产生这条路线:
http://yourdomain.com/<resource>/<id>
请注意,您的代码和下面的代码执行相同的操作并生成相同的路由:
desc "Return a status."
params do
requires :id, type: Integer, desc: "Status id."
end
get ':id' do
Status.find(params[:id])
end
您可以使用 route_param
对接收相同参数的方法进行分组,例如:
resource :categories do
route_param :id do
get do # produces the route GET /categories/:id
end
put do # produces the route PUT /categories/:id
end
end
end
我很难理解 Grape API,特别是 route_param
以及它如何与 params
一起工作。
考虑这段代码:
desc "Return a status."
params do
requires :id, type: Integer, desc: "Status id."
end
route_param :id do
get do
Status.find(param[:id])
end
end
这个街区生产什么路线?我知道这是一个 get
请求,但为什么它包含在 route_param
块中?为什么它不能在 params
块中?
您的街区产生这条路线:
http://yourdomain.com/<resource>/<id>
请注意,您的代码和下面的代码执行相同的操作并生成相同的路由:
desc "Return a status."
params do
requires :id, type: Integer, desc: "Status id."
end
get ':id' do
Status.find(params[:id])
end
您可以使用 route_param
对接收相同参数的方法进行分组,例如:
resource :categories do
route_param :id do
get do # produces the route GET /categories/:id
end
put do # produces the route PUT /categories/:id
end
end
end