2017-03-22 12 views
0

我從外部服務接收這些格式中的任何一種。TypeError:在處理多種哈希時,不會將符號隱式轉換爲整數

#successCase:如果成功/錯誤,這將適當狀態代碼拋出

{:sellerid=>2200, :status=>"success"} 

#failureCase:的意外錯誤我recieving這種散列這也是造成錯誤

{:property=>"instance.seller_mobile", :message=>"does not meet maximum length of 10", :schema=>{:type=>"string", :required=>true, :minLength=>1, :maxLength=>10}, :instance=>"ssss56789", :stack=>"instance.seller_mobile does not meet maximum length of 10"} 

這就是我做的響應,這裏是錯誤發生的地方(我認爲)

`if response[:status] == 'success'` 
#doing something 
elsif response[:status] == 'error' 
#doing something 
end 

如果它的#successCase:迴應這工作正常。但是,如果其#failureCase:我得到錯誤

TypeError: no implicit conversion of Symbol into Integer

我可以看到它在這裏養錯誤Failure/Error: if parsed_response[:status] == 'success'

有人能告訴我如何解決這一問題?

PS: "I tried in console and it seems to be working right but rails throws error"

+0

對於failureCase散列是否有':status'鍵?和/或任何「錯誤」值? –

+0

@SebastiánPalmano .. – Abhilash

+0

你知道Ruby和Rails版本嗎? –

回答

0

我也碰到類似的問題,您可以嘗試獲取響應後調用response.stringify_keys,然後用response["status"] == 'success'代替response[:status] == 'success'

如果你可以在你的外部服務觸及代碼。在響應結果之前,您可以撥打to_json,這會將符號鍵轉換爲字符串鍵。

# in rails console 
> response = {status: 'success', sellerid: 2200} 
=> {:status=>"success", :sellerid=>2200} #symbol key 
> response = {"status" => 'success', "sellerid" => 2200} 
=> {"status"=>"success", "sellerid"=>2200}   
> response.to_json 
=> "{\"status\":\"success\",\"sellerid\":2200}" 

之後,只需撥打response["status"] == 'success'在你身邊。

+0

謝謝你的答案。但這種情況並非如此。我在解析過程中使用符號來防止這種情況。所以'響應[:狀態]'工程..但我認爲這個問題是@MichaelKohl在評論中提到。 – Abhilash

相關問題