尽管在 Heroku 上使用 Rails 设置了新的参数组,但无法在 RDS 中存储 UTF-8
我正在 Heroku 上使用 Amazon RDS 作为数据库设置 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-Type" content="text/html; charset=utf-8" />
另外,我已经设置:
production:
encoding: utf8
collation: utf8_general_ci
...在database.yml中,尽管我不太有信心正在采取任何措施来兑现任何在这种情况下,这些设置,因为 Heroku 在连接到 RDS 时似乎正在执行自己的配置。
现在,我通过应用程序中的表单输入一条注释:“Úbe® ¡iL”,但在数据库中我得到了“Úbe® Æ'àiL”
当 Rails 加载它时,它看起来很好从数据库中返回并将其呈现到页面上,因此无论它以一种方式执行什么操作,都会以另一种方式进行撤消。如果我查看 Sequel Pro 中的 RDS 数据库,如果将编码设置为“UTF-8 Unicode via Latin 1”,看起来就很好。所以看来 Latin-1 正在潜入某个地方。
在开发过程中,当连接到本地 MySQL 数据库时,一切正常。
以前一定有人这样做过,对吧?我缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个更简单的方法。您可以在数据库连接字符串中指定编码。编辑 RDS 附加组件,并附加
?encoding=utf8&collation=utf8_general_ci
对我来说效果很好,项目没有任何更改。
例如:
参考:
http://blog.arvidandersson.se /2011/09/27/setting-activerecord-connection-to-utf8-on-heroku
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.:
Reference:
http://blog.arvidandersson.se/2011/09/27/setting-activerecord-connection-to-utf8-on-heroku
最终,我通过在环境中的 Rails::Initializer.run 块中添加以下内容解决了我的问题。rb
Heroku 覆盖了 database.yml 文件,并且不包含任何编码或联盟设置。通过如此破解,在建立数据库连接之前始终会合并正确的设置。
Ultimately I solved my problem by adding the following in the Rails::Initializer.run block in the environment.rb
Heroku overwrites the database.yml file and doesn't include any encoding or coalition settings. By hacking it thusly, the correct settings are always merged in before the database connection is made.