Ruby on Rails: Fix for ‘invalid multibyte char (US-ASCII)’

You probably got this error message in Ruby on Rails:

/Users/stefan/.../controllers/categories_controller.rb:37: invalid multibyte char (US-ASCII)

The problem is that Ruby 1.9 expects you to use ASCII characters in your source code, and you have used a non ASCII character.

The solution is to tell Ruby that your source file is UTF-8 by including this directive as the first line of your file:


# encoding: utf-8

You probably use UTF-8 for all your files. Once you include a non-ASCII character, the difference between ASCII and UTF-8 becomes relevant for Ruby. That’s when you need this directive.

4 thoughts on “Ruby on Rails: Fix for ‘invalid multibyte char (US-ASCII)’

  1. niyazi

    HELP : linuxmint “katya”+ruby1.9+rails+sqlite3 … the problem is “turkish” characters error like “ı ş ç ö ğ ü ” not display in browser with webrick server… is there any gem for turkish character set ?
    you said,
    # encoding : utf-8
    where i have to add this line?
    thanks

  2. lsrojas

    sample:
    # coding: utf-8
    class HeloController < ApplicationController
    def index
    @mensaje="¡Hola amigo!"
    @contador=3
    @bono="Este mensaje viene desde el controlador"
    end
    end

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>