ruby-on-rails – Erro: incompatible character encodings: UTF-8 and ASCII-8BIT

Question:

Incompatible character encodings: UTF-8 and ASCII-8BIT.

{"utf8"=>"✓",
"authenticity_token"=>"3cl0R8ghLDvPMGkh0I+LCrzQjmyu1donLE9y58rIT1k=",
"post"=>{"title"=>"Somos apaixonados por música!",
"caption"=>"Estamos entrando na fase final da construção do estúdio. Mau podemos esperar para iniciar os trabalhos dos grandes artistas que nos esperam.",
"photo"=>#<ActionDispatch::Http::UploadedFile:0x00000103339718 @original_filename="Captura de Tela 2014-05-30 a\xCC\x80s 11.45.37 (2).png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"post[photo]\"; filename=\"Captura de Tela 2014-05-30 a\xCC\x80s 11.45.37 (2).png\"\r\nContent-Type: image/png\r\n",
@tempfile=#<File:/var/folders/qa/qas8gqrFHKeYuO3C3gst+E+++TI/-Tmp-/RackMultipart20140604-1488-amcll7>>,
"style_photo"=>"1"},
"commit"=>"Cadastrar Notícia"}

PROBLEM:

The problem is in the name of the file I'm trying to upload, it contains an accent ( Screen Capture 2014-05-30 at 11.45.37 (2).png ) and then it is generating this error.

Is there any way to resolve this?

Answer:

Try the following:

file_data = params[:import_file].tempfile.read.force_encoding("ISO-8859-1")
utf8_file_data = file_data.encode(utf8_format)

Taken from this Stack-EN question

Scroll to Top