Special characters in view cause Encoding::CompatibilityError in 1.9.2

193 views
Skip to first unread message

Quintus

unread,
Nov 18, 2010, 12:31:32 PM11/18/10
to sinatrarb
Hi there,

First off, I'm quite new to web programming and Sinatra, so it's
probably me who's doing something wrong.
Here's my problem: When I have a view which in turn renders another
view (a partial) and this second view contains a special character
like ä somewhere (even in a HTML comment!), Sinatra complains with
this error:

Encoding::CompatibilityError at /
incompatible character encodings: US-ASCII and ASCII-8BIT

My directory structure looks like this (all files are encoded as
UTF-8):

/
- my_app.rb
views/
- main.rhtml
- _part.rhtml


Here's my_app.rb:
====================================
#!/usr/bin/env ruby
#Encoding: UTF-8
require "erb"
require "sinatra/base"

class MyApp < Sinatra::Base

set :app_file, __FILE__

set :root, File.dirname(__FILE__)

configure :development do
enable :logging
enable :dump_errors
set :bind, "localhost"
end

get "/" do
erb :main
end

end

MyApp.run! :host => "localhost", :port => 3000
====================================

main.rhtml:
====================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>Test</title>
</head>
<body>
<%= erb :_part %>
<p>Some text with special char ä here</p>
</body>
</html>
====================================

_part.rhtml:
====================================
<p>ä</p>
====================================

(I invoke the application by "ruby my_app.rb")

The special character in main.rhtml always renders fine ("always"
means, if I don't render the partial).

ruby -v: ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
Sinatra version: 1.1.0
OS: Ubuntu 10.10 Maverick Meerkat

Here's the full console output Sinatra produces when just opening
http://localhost:3000 in Firefox: http://pastie.org/1308841

What's wrong here?

Valete,
Marvin

Konstantin Haase

unread,
Nov 18, 2010, 1:44:22 PM11/18/10
to sina...@googlegroups.com
This is a known issue with ERB, see https://github.com/rtomayko/tilt/pull/45
If I see Ryan today, I'll talk to him about it.

Konstantin

> --
> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
>

Quintus

unread,
Nov 18, 2010, 2:15:32 PM11/18/10
to sinatrarb
Thanks Konstantin, I already questioned wheather I was able to read
documentation^^

For the time being, I'll simply apply Skade's patches to my local tilt
installation which will hopefully do the trick. I'll report here again
after doing so tomorrow.

Vale,
Marvin

Quintus

unread,
Nov 18, 2010, 2:52:14 PM11/18/10
to sinatrarb
The patch seems to be against an older version of tilt, but after some
modifications (i.e. applying the patch manually and than producing a
new diff) I got a diff that is applyable: http://pastie.org/1309180

However, it doesn't work. Instead of the previous error I now get this
one:

Encoding::CompatibilityError at /
incompatible character encodings: UTF-8 and ASCII-8BIT

(note that the first encoding mentioned now is UTF-8 and not US-ASCII)

With this console output: http://pastie.org/1309185

Vale,
Marvin

nikola

unread,
Nov 18, 2010, 3:19:31 PM11/18/10
to sina...@googlegroups.com
I'm not sure this is at all applicable to your problem, but we had
similar issue on rails at work and my colleague solved it using
CGI::unescape() on the output.

regards

Konstantin Haase

unread,
Nov 18, 2010, 5:05:08 PM11/18/10
to sina...@googlegroups.com
I'll look into it. Could you maybe open a ticket and gist the code I need to reproduce this?

Konstantin

Quintus

unread,
Nov 20, 2010, 4:33:06 AM11/20/10
to sinatrarb

Francisco Cabrita

unread,
Nov 30, 2010, 7:18:10 PM11/30/10
to sinatrarb
Hi there,

Encoding::CompatibilityError: incompatible character encodings: US-
ASCII and ASCII-8BIT

I'm having this problem on Sinatra 1.1.0 with Ruby 1.9.2-p0.

Just to know if I am missing something or the patch is being cooked :)

Sinatra is too precious to have a bug on my logs :D

many thanks,
Francisco
Reply all
Reply to author
Forward
0 new messages