Heroku and Rails: how to set utf-8 as the default encoding

11,028

Solution 1

In your config/application.rb,

  config.encoding = "utf-8"

in the database.yml,

 development:
     adapter:  mysql2(whatever your db)
     host:     localhost
     encoding: utf8

you also have to add(including the hash)

# encoding: UTF-8

source:http://craiccomputing.blogspot.com/2011/02/rails-utf-8-and-heroku.html

Solution 2

I found this, much easier solution:

Just add ENV['RUBYOPT'] = "-Ku" to your environment variables on Heroku. You can do this with figaro gem:

  1. Add gem "figaro" to your Gemfile
  2. bundle install
  3. Insert this code in config/application.yml:

    production:
      RUBYOPT: "-Ku"
    
  4. Run rake figaro:heroku

Also, you can try with magic_encoding gem, but I dislike this approach.

Share:
11,028
Augusto
Author by

Augusto

Updated on June 04, 2022

Comments

  • Augusto
    Augusto almost 2 years

    Today, I created a copy of a working app, which runs perfectly on Heroku, and tried to deploy it on Heroku as a starting point for a new project.

    I added the new folder as a git repository, created a new remote repository on GitHub, edited the config file and gave new names to the databases, created the new databases and tried to deploy on Heroku.

    Now the app crashed on startup because Heroku finds some utf-8 text inside my source files and doesn't recognize them:

    2011-06-27T14:23:10+00:00 app[web.1]: /app/app/controllers/home_controller.rb:118: invalid multibyte char (US-ASCII)
    2011-06-27T14:23:10+00:00 app[web.1]: /app/app/controllers/home_controller.rb:118: syntax error, unexpected $end, expecting '}'
    2011-06-27T14:23:10+00:00 app[web.1]: ...tue azioni, conquista la città!"}
    

    How can I tell Rails and Heroku that all of my source file are utf-8 encoded? Should I add a UTF-8 BOM in EVERY file? That's crazy and I was not doing so in my previous app that worked beautifully.

    I'm using Rails 2.3.6.

  • Augusto
    Augusto almost 13 years
    Thanks Felix, I have 2 questions: 1. in Rails 2.3.6 I do not have a config/application.rb , where should I put config.encoding = "utf-8" ? 2. where should I put "# encoding: UTF-8"? at the top of each file?
  • felix
    felix almost 13 years
    Oh incase of rails 2.3, you can add them to the environment.rb and you have to add #encoding: utf-8 to the model which is going to store the values.
  • Andrea Salicetti
    Andrea Salicetti over 11 years