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

I have an application that allows users to enter a string, I analyze it, save it to the database for historical purposes and return some messages.

In the controller, this is how I create the messages

@messages << name + " was not created" 

In the view, this is the line in which it is split into

<% @messages.each do |msg| %>
  <li> <b><%= msg %></b></li> <--- this line
<% end %>

Performing a search on the problem led to several solutions and explanations of why the problem occurs. I handle encoding correctly in several places:

  • My app will convert things to UTF8 by default.
  • When I print in Chinese characters and I issue a specific token in the controller, it displays what I entered.
  • When I draw the concatenated string above, it displays the correct string
  • The database is set to UTF-8 encoding
  • ( - ).

, " " , , .

, ,

<li> <b><%= msg.force_encoding("utf-8") %></b></li>

, "" - , , , UTF-8 UTF-8, , UTF-8.

, , :

@messages << name + " was not created" 

,

@messages.size.times do |i|
  @messages[i] = @messages[i].force_encoding("UTF-8")
end

.

?

+4
1

?

#mb_chars , , :

@messages << name.mb_chars + " was not created"

<% @messages.each do |msg| %>
  <li><b><%= msg.mb_chars %></b></li>
<% end %>
+1

All Articles