4

I am interested in creating a website in Hebrew using Ruby on Rails 3. The problem is when I put Hebrew into my view I am told that it is not supported and I should add UTF-8.

I've been working on this for a while and I Can't seem to find how to do this. I am also using Sqlite3 and I would like to save Hebrew strings there too.

How would I achieve this?

The error code I am given is:

Your template was not saved as valid UTF-8. Please either specify UTF-8 as the encoding for your template in your text editor, or mark the template with its encoding by inserting the following as the first line of the template:...

Edit:

Problem was I was working on Notepad++ which did not save my files in UTF-8 format although they were UTF-8 formated files. Solved by changing file format.

Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98

5 Answers5

2

If you are using notepad++, first set the encoding to "Encode in UTF-8" and then start coding. If you have already created/saved the file then just changing the encoding type will not do. You will have to keep a copy of the existing code, then delete the existing file, open notepad++, set the encoding first(Encode in UTF-8) and then start writing/copying the code to it. This way utf-8 encoding is ensured and you won't have to put "# encoding: UTF-8" at the top of your file.

2

You should try adding on the first line of your .rb files the following:

# encoding: utf-8

and on the first line of your .erb

<%# encoding: utf-8 %>

encoding: utf-8 and coding: utf-8 and are equivalent.

Hope this helps.

gicappa
  • 4,862
  • 1
  • 19
  • 23
  • 1
    This won't work on its own unless the file is properly encoded. Apparently Notepad++ has issues with unicode on anything later than Windows XP: http://superuser.com/questions/21135/how-can-i-edit-unicode-text-in-notepad – regularfry Feb 10 '11 at 10:05
2

Make sure that in your database configurations utf-8 is the default character set, and not latin1.
If you use MySQL change it in the "MySQL Server Instance Config Wizard".

EDIT: Try putting this code in your application controller:

class ApplicationController < ActionController::Base
before_filter :set_charset

def set_charset
@headers["Content-Type"] = "text/html; charset=utf-8"
end
end

read more on this article: http://www.dotmana.com/?p=95

Oded Harth
  • 4,367
  • 9
  • 35
  • 62
1

you can put

config.encoding = "utf-8"

in your config/application.rb which is equivalent to

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

which in turn is the equivalent to putting:

# encoding: UTF-8

or a BOM at the top of every file.

This allows utf-8 globally on all files of the rails app. If you want a global option on all ruby files, you can use the -Ku ruby option and set it via the RUBYOPT environment variable, like:

export RUBYOPT=-Ku 
Viktor Trón
  • 8,774
  • 4
  • 45
  • 48
0

This might be caused by the file encoding itself. Make sure you have set UTF-8 as default encoding for project in your editor/IDE preferences.

Edit:

You can check file for encoding with:

file -I myview.erb.html

(that's a capital 'i').

Kamil Sarna
  • 5,993
  • 1
  • 32
  • 22
  • I got voted down but I still encourage you to double check that your file is properly encoded in UTF8. I still think this might be it. – Kamil Sarna Feb 04 '11 at 10:35
  • So notepad++ might be the issue - seems to have issues with the encoding? Surelt Kamil should be reward the correct answer? – macarthy Feb 10 '11 at 15:30