Ruby哈希:无法将字符串转换为整数TypeError

4

目前遇到 Ruby Hash: can't convert String into 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
1个回答

8

JSON.parse(request.body.read) 返回一个哈希的数组。所以解决方法是使用 edit_id = data[0]["downloadID"]。用 p data 替代 puts data,你将会看到 data 是一个哈希的数组。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接