Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails TypeError:没有将符号隐式转换为整数-Ruby on Rails_Ruby On Rails - Fatal编程技术网

Ruby on rails TypeError:没有将符号隐式转换为整数-Ruby on Rails

Ruby on rails TypeError:没有将符号隐式转换为整数-Ruby on Rails,ruby-on-rails,Ruby On Rails,我有一个很基本的问题 我试过解决办法 然而,这对我不起作用 我的代码是: #Logic for connecting to end point and getting the response is above this line... puts res.body Rails.logger.debug "Response from endpoint: #{res.body}" # Once a successful response is received, update the corre

我有一个很基本的问题

我试过解决办法

然而,这对我不起作用

我的代码是:

#Logic for connecting to end point and getting the response is above this line...

puts res.body
Rails.logger.debug "Response from endpoint: #{res.body}"

# Once a successful response is received, update the corresponding service table.

content = Hash.new
Rails.logger.debug "Content initial value: #{content}"

content = res.body
Rails.logger.debug "Content loaded value: #{content}"

if content[:result].length != 0
  if content[:result][:hotel][:@type] == "LodgingReservation"

#More logic below this line . . .
响应的格式,即“res.body”是json,如下所示

{"message": "OK", "code": 200, "id": "de97c01a-b206-11e5-8976-0a5d4c2d1469", "result": {"hotel": {"reservationFor": {"telephone": "+123456", "@type": "LodgingBusiness", "x-days": "6"}, "reservationStatus": "http://schema.org/ReservationConfirmed", "broker": {"@type": "Organization", "name": "xyz"}, "checkoutTime": "2014-04-06T12:00:00", "reservationId": "426396323", "checkinTime": "2014-03-31T15:00:00", "@context": "http://schema.org", "@type": "LodgingReservation"}}}
当我尝试运行代码时,它在这一行给出“TypeError:”--
if content[:result].length!=0

以下是调试器的输出:

Content initial value: {}

Content loaded value: {"message": "OK", "code": 200, "id": "de97c01a-b206-11e5-8976-0a5d4c2d1469", "result": {"hotel": {"reservationFor": {"telephone": "+123456", "@type": "LodgingBusiness", "x-days": "6"}, "reservationStatus": "http://schema.org/ReservationConfirmed", "broker": {"@type": "Organization", "name": "xyz"}, "checkoutTime": "2014-04-06T12:00:00", "reservationId": "426396323", "checkinTime": "2014-03-31T15:00:00", "@context": "http://schema.org", "@type": "LodgingReservation"}}}
从输出中可以看出,“内容”不是散列。我在想为什么

当我在rails控制台中逐行运行此代码时,它正在工作


正如您所能看到的,它在rails控制台上工作得非常完美。有没有办法解决这个奇怪的问题

键正在转换为字符串,请尝试

content["result"].length

这些键正在转换为字符串,请重试

content["result"].length

是的,您的解决了代码行-->if content[:result].length的问题0但是,当我将您的解决方案应用到下一行代码-->if content[:result][:hotel][:@type]==“LodingReservation”时,它给出了以下错误-->NoMethodError:nil:NilClass的未定义方法“[]”。更改后的代码是--->如果内容[“结果”][“酒店”][“@type”]==“住宿预订”严格来说,您应该接受我的回答,并针对新错误发布新问题。您需要进行一些调试-内容[“结果”][“酒店”]返回什么?你确定你保存了新代码,没有输入错误吗?我在控制台中运行了content[“result”][“hotel][“@type”],它返回了“LodingReservation”。是的,您解决了代码行-->if content[:result]的问题.length!=0。但是,当我将您的解决方案应用到下一行代码-->if content[:result][:hotel][:@type]==“LodingReservation”时,它给出了以下错误-->NoMethodError:undefined method`[]'for nil:NilClass。更改后的代码是--->如果内容[“result”][“hotel”][“@type”]=“LodingReservation”严格来说,您应该接受我的回答,并针对新错误发布一个新问题。您需要进行一些调试-内容[“result”][“hotel”]返回什么?您确定保存了新代码,没有输入错误吗?我运行了内容控制台中的[“结果”][“酒店”[“@type”]并返回“住宿预订”。