Often we come across this #FiveWordTechHorror “invalid byte sequence in UTF-8” while working on Ruby projects or projects built on Ruby Framework (Ex : Rails).
So, the question is how to properly encode & decode ?
Use encodeURI or
encodeURIComponent()method to encode.
Ex : Suppose name contains unicode characters.
encoded_name = encodeURI(name)
On ruby side decode it using
Ex: Suppose encoded_name in params is received as params[:encoded_name]
name = CGI::unescape(param[:encoded_name])
When submitted through Rails form :-
CGI::escapemethod to encode it and
CGI::unescapemethod to decode it.
unpackmethod of String class and then
packit using pack method of Array class.