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

Closed
warbot opened this Issue Aug 1, 2013 · 8 comments

Projects

None yet

3 participants

@warbot
warbot commented Aug 1, 2013

Rails 3.2.6, Ubuntu; After switch from 171 to 174 start receiving exception:
'incompatible character encodings: ASCII-8BIT and UTF-8' through the app;

One of the cases, tmp.html.erb:

<%
str = "始"
str.encoding   #=> UTF-8
str.html_safe  #=> exception: 'incompatible character encodings: ASCII-8BIT and UTF-8'
%>
@enebo
Member
enebo commented Aug 1, 2013

If I run this as a script directly this works using master. So either there is something else going on with erb or this has been resolved since 1.7.4. Can you try running this script. If it fails for you then we fixed it. If it works then there is something else going on:

#encoding: utf-8
require 'active_support/core_ext/string/output_safety'

str = ""
p str.encoding   #=> UTF-8
puts str.html_safe  #=> exception: 'incompatible character encodings: ASCII-8BIT and UTF-8
@warbot
warbot commented Aug 1, 2013

@enebo , yep, the script works. Any suggestions on what's is causing the problem of mine?

Thanks.

@enebo
Member
enebo commented Aug 2, 2013

@ted-softserve yeah this is a weird one. str in the erb snippet is in fact a self contained program similar to the snippet I had you run. In my mind the only thing it could be would be that somehow html_safe string is getting transcoded. Are you sure you are not using <%= for this snippet and not <%?

@warbot
warbot commented Aug 6, 2013

@enebo, yayks, sorry, it is <%= indeed.

@enebo
Member
enebo commented Aug 6, 2013

@ted-softserve Can you say which erb library you are using to run this? It could be an out of date version which was not properly dealing with encodings or it is a current issue, but the next step is making sure we make a minimized case using <%=.

@warbot
warbot commented Aug 6, 2013

@enebo action_view(Rails 3.2.6) if I got you right;
I've just made some tests: tag_options, as a example, is raising error, so I've put puts in it just in order to have a look at what''s going on:

puts "value = #{value.inspect}"
puts "value.encoding = #{value.try(:encoding) rescue nil}"

I saw the strings encoded in UTF-8(@jruby171) being encoded in ASCII-8BIT(@jruby174), but not all of them => as a result some pages with unicode are rendered correctly when others are not(exception raised).

@warbot
warbot commented Aug 6, 2013

@enebo, a little bit of investigation:
at jruby174:

irb(main):008:0> s = "备 and a1"
=> "备 and a1"
irb(main):009:0> s.encoding.name
=> "UTF-8"
irb(main):010:0> r = s.encode(s.encoding, :xml => :attr)
=> "\"\xE5\xA4\x87 and a1\""
irb(main):011:0> r.encoding.name
=> "ASCII-8BIT"

at jruby171:

irb(main):010:0> s = "备 and a1"
=> "备 and a1"
irb(main):011:0> s.encoding.name
=> "UTF-8"
irb(main):012:0> r = s.encode(s.encoding, :xml => :attr)
=> "备 and a1"
irb(main):013:0> r.encoding.name
=> "UTF-8"
@headius
Member
headius commented Aug 9, 2013

This will be fixed by my work going into 1.7.5. It already works on master.

@headius headius closed this Aug 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment