0

我正在为 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
4

1 回答 1

3

我认为你在这里有两个问题。第一个在于您的表单声明。在代码中,您说您有一个ssa哈希数组(称为 )(称为ss)。在您的表单中,您正在发送一个称为ss1“ssa”数组的一部分的哈希。哈希将ss1被忽略,因此您的数组中只有一个 'ss' 元素。

如果您在表单中重命名ss1为:ss

ssa[][ss][section_name]   A
ssa[][ss][section_name]   B

你会遇到第二个问题,它存在于 API 控制器定义中:

您的控制器需要一个只能有一个“ss”哈希元素的“ssa”数组。因此它将覆盖第一个[ss][section_name].

您想要做的是将 声明ssa为一个数组并删除该ss组:

requires :ssa, type: Array do
    optional :section_name, type: String
    optional :total_pages, type: Integer
    optional :color_pages, type: String
    optional :score_sheet_id, type: Integer
end

这将需要一个哈希数组 ( ssa)。您不需要声明该组,它已经需要一个带有,等ss的哈希数组作为键。如果不是必需的参数,只需声明它,就像您在控制器中所做的那样。然后,您的表单应如下所示:section_nametotal_pagesssaoptional

ssa[][section_name]                ABC
opportunity[ssa][][total_pages]    3
ssa[][section_name]                DEF
opportunity[ssa][][total_pages]    6

这将导致:

:ssa=>
  [{:section_name=>"DEF",
    :total_pages=>3,
    :color_pages=>nil,
    :score_sheet_id=>nil},
   {:section_name=>"HGJK",
    :total_pages=>6,
    :color_pages=>nil,
    :score_sheet_id=>nil}]
于 2014-06-27T13:46:35.787 回答