5

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?

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • is your view file encoding in UTF-8 ? – medBouzid Nov 24 '13 at 19:37
  • Do you mean the file encoding? Yes, it is UTF-8, but I'm not sure why that would make a difference though. – MxLDevs Nov 24 '13 at 19:46
  • 1
    this article may help you http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/ , to be honest i don't read it but it seem have a great things about encoding :) if you find something useful tell me – medBouzid Nov 24 '13 at 20:05
  • see also this answer http://stackoverflow.com/questions/3223518/ruby-1-9-sinatra-incompatible-character-encodings-ascii-8bit-and-utf-8?rq=1 – medBouzid Nov 24 '13 at 20:08
  • The idea of using `force_encoding` seems rather questionable. – MxLDevs Nov 25 '13 at 16:43
  • What version of Ruby are you using? – messanjah Feb 23 '15 at 01:50

1 Answers1

2

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 %>
EugZol
  • 6,476
  • 22
  • 41