“no implicit conversion of Array into String”

huangapple go评论101阅读模式
英文:

"no implicit conversion of Array into String"

问题

Please 我只是在Ruby on Rails中开发了一个游戏,并且准备部署它。在开发阶段一切正常,但在部署环境中不断出现"ActionView::Template::Error (no implicit conversion of Array into String):"错误。

错误指向了一行代码,即:

def passcode
  JSON.parse(game.passcode)
end

这是JSON.parse之前我的对象的输出:

["ORANGE", "YELLOW", "PURPLE", "GREEN"]

上面的方法输出:

["ORANGE", "YELLOW", "PURPLE", "GREEN"]

在玩家赢或输游戏后,通行码用于获取游戏结果。

def game_outcome
 if !attempts.empty? && passcode == last_attempt
  return "Congratulations!"
 elsif attempts.size == guesses.size && passcode != last_attempt
  return "Sorry, you've lost! It was a bit difficult"
 end
end

我已经尝试在本地机器上查看方法的输出,它输出一个数组,我认为这是正确的事情。

英文:

please I just developed a game in Ruby on Rails, and I am about to deploy it. Everything works fine in the development stage but keeps on getting "ActionView::Template::Error (no implicit conversion of Array into String):" in the deployment environment.
The error pointed me to a particular line of code which is,

def passcode
  JSON.parse(game.passcode)
end

This is the output of my object before the JSON.parse;

"[\"ORANGE\", \"YELLOW\", \"PURPLE\", \"GREEN\"]"

The method above output;

["ORANGE", "YELLOW", "PURPLE", "GREEN"]

The passcode was used to get the game outcome after the player won or lost the game.

def game_outcome
 if !attempts.empty? && passcode == last_attempt
  return "Congratulations!"
 elsif attempts.size == guesses.size && passcode != last_attempt
  return "Sorry, you've lost! It was a bit difficult"
 end
end

I have tried to see the output of the method on my local machine and it outputs an array, which I think it's the right thing

I will appreciate your assistance.

答案1

得分: 0

game.passcode返回一个数组,导致了问题。你能分享一下这个方法是从哪里被调用的吗?

或者你可以尝试转换成JSON来避免这些错误,就像这样:

def passcode
  obj = game.passcode.is_a?(Array) ? game.passcode.to_json : game.passcode
  JSON.parse(obj)
end

或者,如果game.passcode是一个数组,你也可以直接返回它,否则你可以解析JSON。

希望这能解决你的问题。

英文:

The game.passcode is returning an array, which causing the issue. Can you share from where this method is being called.
or

Your can try converting to json to avoid this errors like

def passcode
  obj = game.passcode.is_a?(Array) ? game.passcode.to_json : game.passcode
  JSON.parse(obj)
end

or else you can also directly return the game.passcode if it is an array else you can parse Json.

Hope this will resovle ur issue

huangapple
  • 本文由 发表于 2023年6月6日 15:50:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定