Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting a json array to rails server

I want to know how can I post json array directly to rails server. Say,

POST '/api/notes_bulk', HTTP/1.1
Content-Type: application/json

[{“content”:”content1”, “title”:”title1”}, {“content”:”content2”, “title”:”title2”}, {“content”:”content3”, “title”:”title3”}, {“content”:”content4”, “title”:”title4”}, {“content”:”content5”, “title”:”title5”}]

I did some search about it and all of the examples had some kind of key mapped to the array. While in my case, the json is an array at its top level. How can I get the json data in the controller code? I notice that rails wraps this data with a "_json" key, but when I accessing it, it says
Unpermitted parameters: _json, ....

like image 245
Chris.Zou Avatar asked Jul 18 '15 06:07

Chris.Zou


1 Answers

You can use that '_json' key to access data. To remove the warning you have to permit all the object keys from JSON array. In your case:

params.permit(_json: [:content, :title])
like image 56
shev.vadim.net Avatar answered Oct 13 '22 10:10

shev.vadim.net