I am receiving a formdata request that is an array of objects (object contains file data). The request leaving the browser looks like the below image
I understand why it is being read by rails as objects. An example of the parameters being received by rails is here.
{"id"=>"1",
"user_id"=>"8",
"contact_phone"=>"07969363020",
"about"=>"I am a job seeker",
"trade_ids"=>{"0"=>"1"},
"qualification_ids"=>
{"0"=>
{"expiry_date"=>"2023-01-27T00:00:00.000Z",
"path"=>
#<ActionDispatch::Http::UploadedFile:0x00007fed38c2acb8
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"qualification_ids[0][path]\"; filename=\"facebook.png\"\r\n" + "Content-Type: image/png\r\n",
@original_filename="facebook.png",
@tempfile=#<File:/var/folders/sp/fc_js2cx5lgdmbdqtbng06640000gn/T/RackMultipart20230126-45759-zm83xm.png>>,
"id"=>"1",
"name"=>"CSCS Green Labourer",
"created_at"=>"2023-01-02T19:49:24.941Z",
"updated_at"=>"2023-01-02T19:49:24.941Z",
"temp_path"=>"blob:http://localhost:3000/e83b72ca-f7d9-4cb3-be63-906a11c05357"}}}
I would however like to loop through the qualification_ids as if they were an array.
What would be the best way to go about this?
You can use the each_value
and each_key
methods on Ruby hashes, depending on what you want to iterate through.
Here is an example of a much shorter form to illustrate the usage.
qualification_ids = {"0": {"id": "1"}}
qualification_ids.each_value{|value| puts value}
=> {:id=>"1"}
qualification_ids.each_key{|key| puts key}
=> 0
qualification_ids.each_value{|a| puts a[:id]}
=> 1
I hope this helps:-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.