Ruby Hash: can't convert String into Integer TypeError

5.6k views Asked by At

Currently getting a Ruby Hash: can't convert String into Integer error. The code fails on the edit_id line.

I've tried many different solutions from similar questions already posted on SE, but unfortunately none of them have worked.

Hash:

{"downloadID"=>115, "PageID"=>nil, "title"=>"hi", "dlLink"=>"http://www.a.com", "imgSrc"=>"http://www.a.com", "caption"=>"aaaa", "dlLive"=>nil, "createdAt"=>nil, "user_id"=>7}

Code:

#edit download
put '/view1/downloadedit' do
  data = JSON.parse(request.body.read)
  puts data
  edit_id = data["downloadID"]
  puts edit_id
  @download = Download.get(:download_id => edit_id)
  puts data
  if @download.update(data)
    status 201
    puts 'edit saved okay'
  else
    status 201
    puts 'edit failed to SAVE'
  end
end
1

There are 1 answers

0
Arup Rakshit On BEST ANSWER

JSON.parse(request.body.read) gives you an array of hash. So the fix is edit_id = data[0]["downloadID"]. Write p data instead of puts data, you will see the data is an array of hash.