I have created an API that requires a POST using JSON. This API is used to create multiple new items during a request. I am trying to extract specific information within the JSON shown below.
The JSON structure looks as follows:
{ "Items": [ {"item_id": 1 }, { "item_id": 2 }. { "item_id": 3 }, ... ] }
Inside the controller, I have the following:
def create
all_items = params[:items]
...
# Need something here to extract the item_id's from all_items and
# saved into a variable called item_id
# Possibly a loop to do this
new_item = Item.new(item_id)
new_item.save
render :json => {'Message' => 'Successfully created #{item_id}'}.to_json, :status => 200
end
I have tried using the ActiveSupport::JSON.decode(all_items), however I get an error saying cant convert Array into String. Not sure if I need to use this though.
Your help is much appreciated!
1 Answer