Does a Rails application continue to generate views in ASCII-8BIT when I want UTF-8?

I am trying to make my Rails UTF-8 application native. I do this because from the moment of upgrading to Ruby 1.9 I ran into this problem where every time my web page should display some UTF-8 characters, I get:

incompatible character encodings: UTF-8 and ASCII-8BIT 

as an error page. I diagnosed that an error occurs when I try to display any UTF-8 text, as this type of webpage will fail:

  %h3.book_title = "カタカナ" 

(The encoding request in the above line returns UTF-8)

But this success will be successful:

  %h3.book_title = "カタカナ".force_encoding("ASCII-8BIT") 

So, it looks like Rails decides to display all pages since updating version 1.9 with ASCII-8BIT encoding by default. This is a problem, since my page often presents multilingual data that only works well in UTF-8.

Things I tried:

I am using the Mysql2 gem. I put this in my .rb environment:

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

This is in my .rb application:

 config.encoding = "utf-8" 

None of this works. Can anyone shed some light on how to get my application to use UTF-8 instead of this nonsense ASCII-8BIT?

+4
source share
1 answer

Add the magic character encoding shebang as the first line to the file of your view to force UTF:

 # encoding: UTF-8 %h3.book_title = "カタカナ" 
-2
source

Source: https://habr.com/ru/post/1492366/


All Articles