0

I'm trying to deploy an application on an Ubuntu 14.02 server with NGINX and PUMA and when I try to access static pages on the server with characters in brazilian portuguese and WITHOUT access to the database, the page is not showed and the following error gets logged:

ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8)

I read a lot of posts/answers regarding this error and tried everything.

Ruby version: ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux] Rails version: Rails 4.2.4

The command locale in the server return the following:

LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

My application.rb contains the line:

config.encoding = "utf-8"

My environment.rb contais the lines:

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

I already putted the # encoding: "utf-8" on top of my html.erb files without success.

Diego Senott
  • 176
  • 8

1 Answers1

1

Check that the offending source files actually use the UTF-8 encoding (for example, Textmate allows to save the file using various encodings). Check whether the file is actually recognized by the system as UTF, running file /path/to/something should return UTF-8.

Also if you have just a few of those badly encoded strings you can try something like this:

the_bad_string.dup.force_encoding("UTF-8")

This should convert whatever it was to UTF.

Dmitry Sokurenko
  • 6,042
  • 4
  • 32
  • 53
  • Hi Dmitry, thanks for taking your time to help me! The command file gives me this: divulgue.html.erb: UTF-8 Unicode text The force_encoding didn't help me either. And when I return the code the_bad_string.encoding.name it also gives me UTF-8!!! – Diego Senott Nov 12 '15 at 10:38
  • If your string is originating from somewhere outside of the `divulgue.html.erb` then check the encoding of the source file too. E.g. I've had such problems when I've tried to use the strings defined in `env.rb` in the view. Also check what the `the_bad_string.encoding` returns, is it UTF?. – Dmitry Sokurenko Nov 12 '15 at 12:23
  • No, this is an static page with static content, but with characters like á ã ç. If I remove those characters, the page is rendered fine. I'm using strings defined in a local_env.rb file to define the page title and amazingly they work, even with an ú character in it??!!!??? Yes, the string.encoding.name returns UTF-8. – Diego Senott Nov 12 '15 at 12:32
  • Sounds weird. Try to remove all the offending characters from the file, ensure that this works, and then add just one Portuguese string, edit the file directly on the server, if it doesn't work, and `force_encoding("UTF-8")` doesn't work too, but Portuguese strings from external files work well — consider moving them all into rails i18n .yml files. – Dmitry Sokurenko Nov 12 '15 at 12:40
  • I've accessed my Puma server directly(rails s puma binding=IPADDRESS) and the same page that I'm trying to access through NGINX worked fine! I'm completely lost now! – Diego Senott Nov 13 '15 at 14:01
  • Looks unbelievable, check whether you launch your puma instances the same way when running behind the nginx and when running puma only . Check whether the issue persist when you have nginx and puma running at the same time with nginx talking to the same puma instance that work well. – Dmitry Sokurenko Nov 13 '15 at 14:19