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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Hannes L. (Guest)
on 2007-06-12 12: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
Xavier N. (Guest)
on 2007-06-12 12: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
Hannes L. (Guest)
on 2007-06-12 14:30
Xavier N. 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
This topic is locked and can not be replied to.