UTF-8 encoding in Sinatra


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # Making a Sinatra app respect UTF-8:
  2. #
  3. # 1. At the top of your app file, set $KCODE to 'u'.
  4. # This ensures your regexps are in UTF-8 mode by default,
  5. # and #inspect will output UTF-8 chracters correctly.
  6. # This option is on by default as of Ruby 1.9.
  7. # For more information on the $KCODE setting, see:
  8. # http://blog.grayproductions.net/articles/the_kcode_variable_and_jcode_library
  9. $KCODE = 'u' if RUBY_VERSION < '1.9'
  10.  
  11. # 2. Set content-type with charset=utf-8 param (not the default setting.)
  12. # This ensures the browser will render utf-8 characters correctly.
  13. # A before filter is a good place to do this:
  14. before do
  15. content_type :html, 'charset' => 'utf-8'
  16. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.