"no implicit conversion of Array into String"

603 Views Asked by At

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

There are 1 best solutions below

1
jansha On BEST ANSWER

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