尽管在Heroku上使用Rails设置了新的参数组,但无法在RDS中存储UTF-8 [英] Can't store UTF-8 in RDS despite setting up new Parameter Group using Rails on Heroku

查看:197
本文介绍了尽管在Heroku上使用Rails设置了新的参数组,但无法在RDS中存储UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Amazon RDS作为数据库,在Heroku上设置了一个Rails(2.3.5)应用程序的新实例。我想使用UTF-8处理所有事情。由于默认情况下RDS不是UTF-8,因此我设置了一个新的参数组并切换数据库以使用该参数组,基本上每个这个。似乎已经工作:

  SHOW VARIABLES LIKE'%character%'; 

character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
character_sets_dir /rdsdbbin/mysql-5.1.50.R3/share/mysql/charsets/

另外,我已成功设置Heroku以使用RDS数据库。在rake db:migrate之后,一切看起来都不错:

  CREATE TABLE`comments`(
`id` int(11 )NOT NULL AUTO_INCREMENT,
`commentable_id` int(11)DEFAULT NULL,
`parent_id` int(11)DEFAULT NULL,
`content` text COLLATE utf8_unicode_ci,
`child_count `int(11)DEFAULT'0',
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY(`id`),
KEY` commentable_id`(`commentable_id`),
KEY`index_comments_on_community_id`(`community_id`),
KEY`parent_id`(`parent_id`)
)ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;

在标记中,我包含了:

 < meta http-equiv =Content-Typecontent =text / html; charset = utf-8/> 

另外,我已设定:

 生产:
编码:utf8
整理:utf8_general_ci

...在database.yml中,尽管我并不十分确信在这种情况下正在做任何事情来完成这些设置,正如Heroku在连接到RDS时似乎正在做自己的配置一样。

现在,我通过应用程序中的表单输入评论:Úbe®ƒåiL,但在数据库中我有Ãbebe®Æ Ã¥iL



当Rails从数据库加载它并将其呈现给页面时,它看起来很好,因此无论它是以单向方式,这是相反的方式。如果我在Sequel Pro中查看RDS数据库,如果将编码设置为通过拉丁文1的UTF-8 Unicode,它看起来很好。所以看起来Latin-1在某处潜行。



当连接到本地MySQL数据库时,一切都在开发中。



以前有人必须这样做,对吧?我错过了什么?

解决方案

有一种更简单的方法。您可以在数据库连接字符串中指定编码。编辑RDS插件,然后附加?encoding = utf8& collat​​ion = utf8_general_ci



没有改变项目。



例如:

  mysql:/ /user:pass@abc.rds.amazonaws.com/my-db?encoding=utf8&collat​​ion=utf8_general_ci 

参考:
http://blog.arvidandersson.se/2011/09/27/setting-activerecord-connection-to-utf8-on-heroku


I'm setting up a new instance of a Rails(2.3.5) app on Heroku using Amazon RDS as the database. I'd like to use UTF-8 for everything. Since RDS isn't UTF-8 by default, I set up a new Parameter Group and switched the database to use that one, basically per this. Seems to have worked:

SHOW VARIABLES LIKE '%character%';

character_set_client        utf8
character_set_connection    utf8
character_set_database    utf8
character_set_filesystem    binary
character_set_results      utf8
character_set_server        utf8
character_set_system        utf8
character_sets_dir       /rdsdbbin/mysql-5.1.50.R3/share/mysql/charsets/

Furthermore, I've successfully setup Heroku to use the RDS database. After rake db:migrate, everything looks good:

CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `commentable_id` int(11) DEFAULT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `content` text COLLATE utf8_unicode_ci,
  `child_count` int(11) DEFAULT '0',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `commentable_id` (`commentable_id`),
  KEY `index_comments_on_community_id` (`community_id`),
  KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

In the markup, I've included:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Also, I've set:

production:
  encoding: utf8
  collation: utf8_general_ci

...in the database.yml, though I'm not very confident that anything is being done to honor any of those settings in this case, as Heroku seems to be doing its own config when connecting to RDS.

Now, I enter a comment through the form in the app: "Úbe® ƒåiL", but in the database I've got "Úbe® Æ’Ã¥iL"

It looks fine when Rails loads it back out of the database and it is rendered to the page, so whatever it is doing one way, it's undoing the other way. If I look at the RDS database in Sequel Pro, it looks fine if I set the encoding to "UTF-8 Unicode via Latin 1". So it seems Latin-1 is sneaking in there somewhere.

Everything works in development, when connecting to a local MySQL database.

Somebody must have done this before, right? What am I missing?

解决方案

There's a simpler way. You can specify the encoding in your DB connection string. Edit the RDS add-on, and append ?encoding=utf8&collation=utf8_general_ci

Worked well for me, no changes to the project.

e.g.:

  mysql://user:pass@abc.rds.amazonaws.com/my-db?encoding=utf8&collation=utf8_general_ci

Reference: http://blog.arvidandersson.se/2011/09/27/setting-activerecord-connection-to-utf8-on-heroku

这篇关于尽管在Heroku上使用Rails设置了新的参数组,但无法在RDS中存储UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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