rails 3 : german umlauts in controller

Hi everybody,

with rails 3 I have the problem that the controllers don't accept german umlaute anymore. E.g. when I try to build an array:

landschaft = %w[ Berg Hügel Tal Ebene]

I get this lovely error message: ... controller.rb:5: invalid multibyte char (US-ASCII)

Sure enough the document encoding of the controller is utf8.

Can anyone of You point me to the right direction?

Thanks and greetings Sven

P.S.: I am testing with ruby 1.9.2 and rails 3 rc2

Ruby 1.9 doesn't interpret the source file as UTF-8 unless it is specified at the top of the file like this:

# encoding: utf-8

Try adding it to the first line of your controller file.

Tor Erik

Sven Koesling wrote:

Hi everybody,

with rails 3 I have the problem that the controllers don't accept german umlaute anymore. E.g. when I try to build an array:

landschaft = %w[ Berg Hügel Tal Ebene]

[...]

This isn't directly related to your problem, but...why on earth is this stuff in your controller? It should be in the DB, or failing that, in an initializer or model.

Best,

Hello Tor, the enconding - hint works perfectly, thank You very much! What leads me to the question why rails 1.9 interprets files as us-ascii - is this a good idea?

And hello Marnen, it was just an example, I came across when I tried to find an error in my application. I was working on the controller and just needed an array. But as I am a beginner I am absolutely sure I would have left things like that in the controller instead of putting them where they belong. So thank You very much too for reminding me.

But no matter if I put my array in the controller or model I need the # encoding: utf-8 line.

Conclusion for all non-US-collegues: The encoding problem often happens, when it comes to notices or error-messages I want to show to the user. Even if I know that my application will be in german it's worth it to leave those messages in english and use the localization-files (in my case de.yml) for the text with special character encodings.

Thanks again, lesson learned :slight_smile: greetings Sven

Sven Koesling wrote:

Hello Tor, the enconding - hint works perfectly, thank You very much! What leads me to the question why rails 1.9 interprets files as us-ascii - is this a good idea?

If I understand correctly, this is more of a core Ruby 1.9 issue than a Rails issue. Ruby 1.9 changed its encoding handling significantly -- probably for the better, but it *is* more complex than 1.8, from what I understand. (Take this with a grain of salt. I haven't used 1.9 yet.)

And hello Marnen, it was just an example, I came across when I tried to find an error in my application. I was working on the controller and just needed an array. But as I am a beginner I am absolutely sure I would have left things like that in the controller instead of putting them where they belong. So thank You very much too for reminding me.

But no matter if I put my array in the controller or model I need the # encoding: utf-8 line.

Right. If it comes from the DB, you don't, at least not that way. In that case, you need to make sure your DB encoding is correct.

Conclusion for all non-US-collegues: The encoding problem often happens, when it comes to notices or error-messages I want to show to the user. Even if I know that my application will be in german it's worth it to leave those messages in english and use the localization-files (in my case de.yml) for the text with special character encodings.

That's true, but for reasons that have little to do with encoding, and more with application structure.

(Of course, I believe that all text and source code files should be UTF-8 anyway.)

Thanks again, lesson learned :slight_smile: greetings Sven

Best,