Encoding problems in rails on ruby 1.9.1

后端 未结 3 1671
一生所求
一生所求 2021-02-15 02:29

I am using rails 2.3.3 and ruby 1.9.1.

I am trying to render a view that includes a partial. In the partial i output a field of a model that is encoded in UTF8. This fai

相关标签:
3条回答
  • 2021-02-15 03:09

    There appears to be an issue with ERB's encoding in Ruby 1.9. More details are in this Lighthouse ticket. A patch with a workaround has been included, perhaps it works for you?

    The problem is erb code in ruby 1.9 distribution. When it compiles the template code it forces a 'ASCII-8bit' encoding, the problem is when the template code has multibyte characters the template code is returned in a 'ASCII-8bit' string and when this string is concat with a 'UTF8' string with multibyte character the exception is raised because the strings between this encodings are only compatible when both only have seven-bit characters.

    0 讨论(0)
  • 2021-02-15 03:24

    I just had this as well so I think its worth having the correct answer.

    The 2.8.1 MySql gem is not utf-8 friendly, so it sometimes will return UTF strings and lie to Rails, telling it that they are ASCII when in fact they are UTF-8. This makes things explode.

    So: you can either monkey patch or get a compatible MySql gem. See: http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/

    0 讨论(0)
  • 2021-02-15 03:27

    There seems to be an incompatibility between Ruby 1.9x and the mysql gem with regard to how strings are passed back and forth (specifically the encoding of the strings).

    To fix, run

    gem install mysql2 
    

    on the server and update the database configuration file to use this gem instead of the previous one.

    0 讨论(0)
提交回复
热议问题