# Rails 3 : german umlauts in controller

**URL:** <https://www.ruby-forum.com/t/rails-3-german-umlauts-in-controller/194072>\
**Category:** Rails\
**Created:** [August 29, 2010, 9:00pm UTC](https://www.ruby-forum.com/t/rails-3-german-umlauts-in-controller/194072 "2010-08-29T21:00:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fink](https://www.ruby-forum.com/u/fink)\
**Post date:** [August 29, 2010, 9:00pm UTC](https://www.ruby-forum.com/t/rails-3-german-umlauts-in-controller/194072/1 "2010-08-29T21:00:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fink](https://www.ruby-forum.com/u/fink)\
**Post date:** [August 30, 2010, 5:00am UTC](https://www.ruby-forum.com/t/rails-3-german-umlauts-in-controller/194072/2 "2010-08-30T05:00:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fink](https://www.ruby-forum.com/u/fink)\
**Post date:** [August 30, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/rails-3-german-umlauts-in-controller/194072/3 "2010-08-30T18:21:31Z")

</div>

Sven K. 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,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![fink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fink](https://www.ruby-forum.com/u/fink)\
**Post date:** [September 1, 2010, 9:37am UTC](https://www.ruby-forum.com/t/rails-3-german-umlauts-in-controller/194072/4 "2010-09-01T09:37:52Z")

</div>

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 🙂  
greetings  
Sven

---

<div class="post-metadata">

**Author:** ![fink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/85f322/32.png) [@fink](https://www.ruby-forum.com/u/fink)\
**Post date:** [September 1, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/rails-3-german-umlauts-in-controller/194072/5 "2010-09-01T15:54:45Z")

</div>

Sven K. 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 🙂  
> greetings  
> Sven

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
