Search code examples
ruby-on-railsencodingutf-8iso-8859-1

Ruby on Rails - Generated Files' Encoding


I have discovered that all the files RoR generates when creating a new application seem to be saved with Western(ISO-8859-1) encoding (I am developing locally on a Windows machine). As a result I am having problems when using certain special characters eg £.

  • Should RoR be generating the framework files and saving them with Western(ISO-8859-1) encoding?
  • If not, how can I get RoR to generate the files and save them with UTF-8 encoding, and so avoid problems with certain characters eg £?

Please see Ruby on Rails - £ sign troubles for a previous unresolved question I asked relating to this problem.


Solution

  • Rails only uses ASCII characters in generated files.

    ASCII files are neither UTF-8 nor ISO-8859-1. ASCII is compatible with both encodings, but an ASCII file doesn't become an ISO-8859-1 or UTF-8 file until you add a special character to it.

    When you save a file after adding a £ character, you should make sure to set up your editor or IDE to use UTF-8 instead of ISO-8859-1. You should look for a configuration option in your editor. Rails cannot do anything about it.

    If you run Ruby 1.9, also remember to set a magic comment at the top of a file containing special characters (except in templates). In Ruby 1.8 and previous versions, this comment has no effect.

    # encoding: utf-8
    

    The exact same problem causes the symptoms you describe in your other question.

    For some background, see this (old but excellent) article about character encodings and Unicode.