ruby 1.9.1 上的 rails 编码问题 [英] Encoding problems in rails on ruby 1.9.1

查看:38
本文介绍了ruby 1.9.1 上的 rails 编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 rails 2.3.3 和 ruby​​ 1.9.1.

I am using rails 2.3.3 and ruby 1.9.1.

我正在尝试呈现包含部分的视图.在部分中,我输出以 UTF8 编码的模型字段.这失败了

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 fails with

ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-8) on line #248 of app/views/movie/show.html.erb:
245:    <!-- Coloumn right | start -->
246:    <div class="col_right">
247: 
248:        <%= render :partial => 'movie_stats' %>
249: 
250:        <!-- uploaders -->
251:        <div class="box_white">     

另一方面,如果我直接在视图中使用该字段(当它不在部分中时),我可以用 utf8 内容输出该字段.

On the other hand, i can output the field with utf8 content just fine if i directly use that field in a view (when it is not in a partial).

我该如何解决这个问题?我已经尝试过设置默认编码,但这似乎不起作用.

How can i fix this? I already tried setting the default encoding but that did not seem to work.

推荐答案

我刚刚也遇到了这个问题,所以我认为它值得拥有正确的答案.

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

2.8.1 MySql gem 不是 utf-8 友好的,所以它有时会返回 UTF 字符串并向 Rails 撒谎,告诉它它们是 ASCII 而实际上它们是 UTF-8.这让事情变得爆炸.

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.

因此:您可以使用猴子补丁或获取兼容的 MySql gem.请参阅:http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/

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

这篇关于ruby 1.9.1 上的 rails 编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆