简体   繁体   中英

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

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

In the controller, this is how I build the messages

@messages << name + " was not created" 

In the view, this is the line that it's crashing on

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

Doing a search on the issue resulted in several solutions and explanations of why the problem occurs. I am properly handling encoding in several places:

  • My application by default converts things to UTF8.
  • When I type in chinese characters and I render the specific token in the controller, it displays what I typed in.
  • When I render the concatenated string above, it displays the correct string
  • The database is set to UTF-8 encoding
  • Other pages are able to render the text correctly (they fetch from the database and display it directly without any sort of string manipulation on my part)

The issue disappears when I comment out "this line" in the View, but I don't understand what is wrong with it.

If I write this, following another suggestion, it works

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

But I don't like it, since I shouldn't be having to "force" any encodings when ideally everything going in should be UTF-8 or properly converted to UTF-8, and the views can assume everything they are rendering is proper UTF-8.

I suspect that the problem is the way I am concatenating the string:

@messages << name + " was not created" 

If I do a force encoding like this

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

That also works.

What is the proper way to concantenate strings?

What is the proper way to concantenate strings?

Using #mb_chars everywhere seems to solve such kind of issues:

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

And

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM