Abdul Baig
Abdul Baig

Reputation: 3721

how to make array of objects for grape

I am building APIs using Grape Api for a rails application.

What I am trying right now is this form:enter image description here

And this is output:

{
    "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"
                }

    }
}

I have omitted some part of json to make it shorter.

What I need is to have an array or ssa but somehow unable to make it till now. It makes an array of ssa with only one object.

While in my API controller I have following code:

    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

Upvotes: 0

Views: 5415

Answers (1)

Mihai Ionescu
Mihai Ionescu

Reputation: 978

I think you have 2 problems here. First one lies in you form declaration. In the code you say you have an Array (called ssa) of Hashes (called ss). In your form, you're sending a hash called ss1 as part of your 'ssa' Array. The ss1 hash will be ignored, thus you'll only have one 'ss' element in you array.

If you rename the ss1 into ss in your form:

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

you'll get to the second problem, which lies in the API controller definition:

Your controller expects a 'ssa' array that can only have one 'ss' hash element. Thus it will overwrite the first [ss][section_name].

What you want to do is declare the ssa as a an Array and remove the ss group:

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

This will require an array (ssa) of hashes. You don't need to declare the ss group, it already expects an array of hashes with section_name, total_pages, etc. as keys. If ssa is not a required param, simply declare it optional, as you did in your controller. Then, your form should look like this:

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

This will result in:

: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}]

Upvotes: 3

Related Questions