Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Hash: can't convert String into Integer TypeError

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
like image 624
Phil Hudson Avatar asked Feb 15 '23 10:02

Phil Hudson


1 Answers

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.

like image 88
Arup Rakshit Avatar answered Mar 12 '23 14:03

Arup Rakshit