Setting charset with a before filter in Sinatra

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

Posted in  | 2 comments

Comments

  1. Avatar John said 34 days later:

    The original post can be found here: http://geminstallthat.wordpress.com/2009/04/22/sinatra-utf-8-content-type-before-filter/

  2. Avatar Jonas said 34 days later:

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


Email will not be shown. Enter email to show Gravatar.

   Comment Markup Help Preview comment