Return 嵌套数组 Ruby 葡萄在 Rails 5
Return nested array with Ruby Grape in Rails 5
我已经成功构建了一个 api,如您所见 here,代码如下。我需要将对象列为嵌套数组而不是对象数组,但我无法弄明白。
api/v1/plaques.rb
module API
module V1
class Plaques < Grape::API
include API::V1::Defaults
resource :plaques do
desc 'Return all Plaques'
get '', root: :plaques do
Plaque.all
end
desc 'Return a Plaque'
params do
requires :id, type: String, desc: 'ID of Plaque'
end
get ':id', root: 'plaque' do
Plaque.where(id: permitted_params[:id]).first!
end
end
end
end
end
我得到的:
[
{
"veteran_first": "Alexis",
...
}
]
我需要的:
"items" = [
{
"veteran_first": "Alexis",
...
}
]
此代码returnsJSON你要:
get '', root: :plaques do
{ items: Plaque.all }
end
我已经成功构建了一个 api,如您所见 here,代码如下。我需要将对象列为嵌套数组而不是对象数组,但我无法弄明白。
api/v1/plaques.rb
module API
module V1
class Plaques < Grape::API
include API::V1::Defaults
resource :plaques do
desc 'Return all Plaques'
get '', root: :plaques do
Plaque.all
end
desc 'Return a Plaque'
params do
requires :id, type: String, desc: 'ID of Plaque'
end
get ':id', root: 'plaque' do
Plaque.where(id: permitted_params[:id]).first!
end
end
end
end
end
我得到的:
[
{
"veteran_first": "Alexis",
...
}
]
我需要的:
"items" = [
{
"veteran_first": "Alexis",
...
}
]
此代码returnsJSON你要:
get '', root: :plaques do
{ items: Plaque.all }
end