Setting charset with a before filter in Sinatra 2

Posted by Jonas Elfström Fri, 31 Jul 2009 06:30:00 GMT

In the git example in my previous post about Sinatra and Heroku I happened to mention a filter for using UTF-8. I remember finding it somewhere on the net but I can't remember where and it seems my current google-fu is as weak as my memory. Anyway, it looks like this:

1
2
3
4
5
6
7
8
9
10
11
CONTENT_TYPES = {:html => 'text/html', :css => 'text/css', 
                :js  => 'application/javascript'}

before do
 request_uri = case request.env['REQUEST_URI']
   when /\.css$/ : :css
   when /\.js$/  : :js
   else          :html
 end
 content_type CONTENT_TYPES[request_uri], :charset => 'utf-8'
end
Comments

Leave a response

  1. Avatar
    John Thu, 03 Sep 2009 08:20:14 GMT
  2. Avatar
    Jonas Thu, 03 Sep 2009 14:21:40 GMT

    Yes, that’s it. Thanks! Credits where credit is due.

Comments