我正在为 Rails 应用程序使用 Grape Api 构建 API。
我现在正在尝试的是这种形式:
这是输出:
{
"page_score_master": {
"issue_date": "2014-06-23"
},
"press_id": "1",
"print_date": "2014-06-23",
"product_id": 1,
"pull_id": 2,
"press_run_id": 1,
"total_section": 1,
"ssa": [
{
"ss": {
"section_name": "A"
},
"ss1": {
"section_name": "B"
}
}
],
"foreman_id": 1,
"pic_id": 1,
"score_sheet_master_id": 1,
"score_sheet_sections_attributes": {
"score_sheet_id": "1"
},
"route_info": {
"options": {
"description": "create score sheet",
"params": {
"page_score_master": {
"required": true,
"type": "Hash"
},
"page_score_master[issue_date]": {
"required": true,
"type": "String"
},
"print_date": {
"required": true,
"type": "String"
},
"total_section": {
"required": true,
"type": "Integer"
},
"ssa": {
"required": false,
"type": "Array"
},
"ssa[section_name]": {
"required": false,
"type": "String"
},
"ssa[total_pages]": {
"required": false,
"type": "Integer"
},
"ssa[color_pages]": {
"required": false,
"type": "String"
},
"ssa[score_sheet_id]": {
"required": false,
"type": "Integer"
}
}
}
我省略了 json 的某些部分以使其更短。
我需要的是有一个数组,或者ssa
直到现在都无法做到。它制作了一个ssa
只有一个对象的数组。
在我的 API 控制器中,我有以下代码:
optional :ssa, type: Array do
requires :ss, type: Hash do
optional :section_name, type: String
optional :total_pages, type: Integer
optional :color_pages, type: String
optional :score_sheet_id, type: Integer
end
end