ruby - how to make array of objects for grape -



ruby - how to make array of objects for grape -

i building apis using grape api rails application.

what trying right form:

and 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 part of json create shorter.

what need have array or ssa somehow unable create till now. makes array of ssa 1 object.

while in api controller have next code:

optional :ssa, type: array requires :ss, type: hash optional :section_name, type: string optional :total_pages, type: integer optional :color_pages, type: string optional :score_sheet_id, type: integer end end

i think have 2 problems here. first 1 lies in form declaration. in code have array (called ssa) of hashes (called ss). in form, you're sending hash called ss1 part of 'ssa' array. ss1 hash ignored, you'll have 1 'ss' element in array.

if rename ss1 ss in form:

ssa[][ss][section_name] ssa[][ss][section_name] b

you'll sec problem, lies in api controller definition:

your controller expects 'ssa' array can have 1 'ss' hash element. overwrite first [ss][section_name].

what want declare ssa array , remove ss group:

requires :ssa, type: array optional :section_name, type: string optional :total_pages, type: integer optional :color_pages, type: string optional :score_sheet_id, type: integer end

this require array (ssa) of hashes. don't need declare ss group, expects array of hashes section_name, total_pages, etc. keys. if ssa not required param, declare optional, did in controller. then, form should this:

ssa[][section_name] abc opportunity[ssa][][total_pages] 3 ssa[][section_name] def opportunity[ssa][][total_pages] 6

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

ruby rest parameters grape grape-api

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -