惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
Y
Y Combinator Blog
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
MyScale Blog
MyScale Blog
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
量子位
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
Lohrmann on Cybersecurity
L
LINUX DO - 最新话题
The Register - Security
The Register - Security
T
Tailwind CSS Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
Cloudbric
Cloudbric
S
Secure Thoughts
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
L
LangChain Blog
Recorded Future
Recorded Future
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
人人都是产品经理
人人都是产品经理
F
Full Disclosure
O
OpenAI News
Webroot Blog
Webroot Blog
A
Arctic Wolf
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
H
Heimdal Security Blog
B
Blog RSS Feed
Vercel News
Vercel News

博客园 - 红马天下

【原创】改造rails使支持gb2312编码 【原创】MBCS,Unicode,UTF8以及一个用C++读写UTF8文件的工具类 【原创】Asp.Net从Windows Server 2003 32bit迁移到Windows Server 2008 64bit的找不到组件 这辈子一定要做一件有意义的事 【转载】软件缺陷管理基本概念 【转载】软件配置管理中的分支 【转载】我的Lean & Agile(精简和敏捷)经历(这才是最佳实践) 【转载】微软的管理模式真的值得吹嘘么? 【转载】负载均衡技术全攻略 【转载】MySpace的六次重构 【转载】转一篇自己的书评,关于《重构》 【转载】避免软件重建失败的三种方案 【转载】ASP.NET AJAX进度条 【转载】快速、可伸缩和流式的AJAX代理--跨域持续内容分发 给CHtmlEditView添加自定义拖拽功能IDropTargetpdwEffectDROPEFFECT_COPY ole拖放的若干tips 深入分析eXtreme Toolkit Toolbar的Command消息 【转载】让文档模板支持多文件扩展名 在eXtreme Toolkit Pro 11.2.1中使用中文
【转载】Rails使用gb2312/gbk与utf8共存的解决方案
红马天下 · 2008-08-12 · via 博客园 - 红马天下

出处:http://xuxu.name/b-4755e670-2cbd-4488-8b4f-a04a1c022fea.htm

作者:xuxu

最近做个项目,需要数据库是UTF-8编码,而Rails程序使用GBK编码。(别问为什么)

那么如何实现这两种编码形式的共存呢?

首先,在创建数据库的时候,要保证数据库是UTF8编码的,然后在rails项目的conf/database.yml中数据库链接字符串加上encoding: utf8 , 这样保证用utf8的形式来读取数据库中的数据。

然后在IDE中(我的是Apatana),“窗口->首选项->工作空间”找到文本文件编码设置,设置为默认的GBK(默认的。。)

修改项目的 config/environment.rb 文件,在最后增加两行:

$KCODE ='n'
require 'jcode'

注意,一定要在最后加,否则不起作用!

然后修改app/controllers/application.rb文件,增加如下行:

before_filter :configure_charsets
def configure_charsets
response.headers["Content-Type"] = "text/html; charset=gb2312"
end

同时还要保证其他的html文件中也将编码设置成了gb2312格式。

还要注意,若原有的存在utf8格式的view文件,也需要转成gb2312格式的。

然后就是最关键的一步!修改ruby的安装目录下的文件(假设安装在了C盘):

找到 C:"ruby"lib"ruby"1.8"xsd"charset.rb文件,

找到下面的代码

代码
CharsetMap = {
'NONE' => 'us-ascii',
'EUC' => 'euc-jp',
'SJIS' => 'shift_jis',
'UTF8' => 'utf-8',
'X_ISO_8859_1' => 'iso-8859-1',
'X_UNKNOWN' => nil,
}

修改为

代码
CharsetMap = {
'NONE' => 'gb2312',
'EUC' => 'euc-jp',
'SJIS' => 'shift_jis',
'UTF8' => 'utf-8',
'X_ISO_8859_1' => 'iso-8859-1',
'X_UNKNOWN' => nil,
}

这样就大功告成了!针对那些在rhtml中存在“删除”字样的情况就出错 的解决方案!

别忘记重新启动一下项目哦~

呵呵