当前位置 : 主页 > 编程语言 > ruby >

Ruby Hash:无法将String转换为Integer TypeError

来源:互联网 收集:自由互联 发布时间:2021-06-23
目前获取 Ruby Hash:无法将String转换为Integer错误.代码在edit_id行上失败. 我已经尝试过许多不同的解决方案,这些解决方案来自SE上已发布的类似问题,但不幸的是,它们都没有奏效 哈希:
目前获取 Ruby Hash:无法将String转换为Integer错误.代码在edit_id行上失败.

我已经尝试过许多不同的解决方案,这些解决方案来自SE上已发布的类似问题,但不幸的是,它们都没有奏效

哈希:

{"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}

码:

#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
JSON.parse(request.body.read)为您提供了一个哈希数组.所以修复是edit_id = data [0] [“downloadID”].写p数据而不是put数据,你会看到数据是一个hash数组.
网友评论