ruby - make json from params hash -
i trying call api , pass data form
in view
controller
, call api post
method.
what in params
is:
parameters: {"utf8"=>"✓", "authenticity_token"=>"kllavrc6kcndkfb1nkntstzgcg95cmct1uvencqwlgc=", "score_sheet"=>{ "score_sheet_master"=>{ "issue_date(1i)"=>"2014", "issue_date(2i)"=>"6", "issue_date(3i)"=>"27"}, "print_date(1i)"=>"2014", "print_date(2i)"=>"6", "print_date(3i)"=>"27", "product_id"=>"2", "pull_id"=>"2", "press_run_id"=>"1", "total_section"=>"2", "score_sheet_sections_attributes"=>{ "0"=>{"section_name"=>"a", "total_pages"=>"2", "color_pages"=>"1", "_destroy"=>"false"}, "1"=>{"section_name"=>"b", "total_pages"=>"1", "color_pages"=>"", "_destroy"=>"false"}}, "color_pages_rated"=>"1", "bw_pages_rated"=>"2", "foreman_id"=>"", "pic_id"=>""}, "score_sheet_master"=>{ "press_id"=>"1"}, "commit"=>"create score sheet"}
i want hash converted following json
apis
accept. i-e.
{ "score_sheet": { "score_sheet_master": { "issue_date": "2014-06-25", "press_id": "1" }, "print_date": "2014-06-23", "product_id": 1, "pull_id": 2, "press_run_id": 1, "total_section": 2, "score_sheet_sections_attributes": [ { "section_name": "a", "total_pages": 3, "color_pages": "2,3", "id": 9 }, { "section_name": "b", "total_pages": 1, "color_pages": "1", "id": 10 }, { "section_name": "c", "total_pages": 2, "color_pages": "2" } ], "pic_id": 1, "foreman_id": 1 } }
some fields in json might missing or may more hash don't have concern handle them main issue convert params hash required json. hold on have implemented date conversion
thing working fine. don't know how convert score_sheet_sections_attributes
array format required format.
oh got myself:
sections = [] params["score_sheet"]["score_sheet_sections_attributes"].each {|k,v| sections << v} params["score_sheet"]["score_sheet_sections_attributes"] = sections
which gives:
{"utf8"=>"✓", "authenticity_token"=>"kllavrc6kcndkfb1nkntstzgcg95cmct1uvencqwlgc=", "score_sheet"=>{"score_sheet_master"=>{"issue_date(1i)"=>"2014", "issue_date(2i)"=>"6", "issue_date(3i)"=>"27"}, "print_date(1i)"=>"2014", "print_date(2i)"=>"6", "print_date(3i)"=>"27", "product_id"=>"2", "pull_id"=>"2", "press_run_id"=>"1", "total_section"=>"2", "score_sheet_sections_attributes"=>[{"section_name"=>"a", "total_pages"=>"2", "color_pages"=>"1", "_destroy"=>"false"}, {"section_name"=>"b", "total_pages"=>"1", "color_pages"=>"", "_destroy"=>"false"}], "color_pages_rated"=>"1", "bw_pages_rated"=>"2", "foreman_id"=>"", "pic_id"=>""}, "score_sheet_master"=>{"press_id"=>"1"}, "commit"=>"create score sheet"}
and dates have logic implemented , done.
Comments
Post a Comment