Forum: Ruby on Rails How do I change character encoding from UTF-8 to ISO-8859-1?

Posted by Hannes Larsson (hannes)
on 2007-06-12 10:14
Hi!

I have a Rails application that is using a MSSQL server/database. I need
to change the character set encoding to ISO-8859-1. How do I do this???

I have added the defult_cahrset to the environment.rb like:
ActionController::Base.default_charset=('ISO-8859-1')

And I have set the attribute on the meta tag in the header like:
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"
/>

And also, since I am using the Aptana RadRails IDE I have set the
text-file encoding property to ISO-8859-1.

But when I try to populate my database using the migration scripts I get
an error saying:
syntax error, unexpected tSTRING_BEG
unterminated string meets end of file

The line of code contains Swedish characters like Å, Ä and Ö. If i
remove the Ö, all is fine. But this is not an option.

Need help fast!!!

Thanks!
Hannes
Posted by Xavier Noria (Guest)
on 2007-06-12 10:56
(Received via mailing list)
On Jun 12, 2007, at 10:14 AM, Hannes Larsson wrote:

> The line of code contains Swedish characters like Å, Ä and Ö. If i
> remove the Ö, all is fine. But this is not an option.

Rails sets $KCODE to "UTF8" by default since 1.2.

Thus, unless you set $KCODE to something else the Ruby interpreter
expects source code in UTF-8. That is, your editor is using latin1 to
write the file just fine, but Ruby still expects UTF-8 because you
configure that option in the interpreter via $KCODE. Just assign

   $KCODE = 'NONE'

in environment.rb.

Additionally, it would be healthy to explicitely configure the
database driver as well in database.yml, in MySQL that would be for
example

   encoding: latin1

-- fxn
Posted by Hannes Larsson (hannes)
on 2007-06-12 12:30
Xavier Noria wrote:
> On Jun 12, 2007, at 10:14 AM, Hannes Larsson wrote:
> 
>> The line of code contains Swedish characters like �, � and �. If i
>> remove the �, all is fine. But this is not an option.
> 
> Rails sets $KCODE to "UTF8" by default since 1.2.
> 
> Thus, unless you set $KCODE to something else the Ruby interpreter
> expects source code in UTF-8. That is, your editor is using latin1 to
> write the file just fine, but Ruby still expects UTF-8 because you
> configure that option in the interpreter via $KCODE. Just assign
> 
>    $KCODE = 'NONE'
> 
> in environment.rb.
> 
> Additionally, it would be healthy to explicitely configure the
> database driver as well in database.yml, in MySQL that would be for
> example
> 
>    encoding: latin1
> 
> -- fxn

Thanx a million!

added $KCODE = 'NONE' to environment.rb, now it all works as it suppose 
to! super and thanx again!

/Hannes
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.