-1

I'm using the following code to show description in template:

json.description resource.description if resource.description.present?

It gives me invalid byte sequence in UTF-8 error. I dig this a little bit, and find out the issue is my description has single quote as ’ instead of '. Wondering what's the best way to fix this encoding issue? Description is input by user and I have no control over it. Another weird issue is, I have multiple test environments, they all have the same Ruby and Rails version and they are running the same code, but only one of the environment has this error.

Fei Qu
  • 121
  • 1
  • 9

1 Answers1

0
def to_utf8(str)
  str = str.force_encoding("UTF-8")
  return str if str.valid_encoding?
  str = str.force_encoding("BINARY")
  str.encode("UTF-8", invalid: :replace, undef: :replace)
end

ref: https://stackoverflow.com/a/17028706/455770

Rots
  • 5,506
  • 3
  • 43
  • 51