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

推荐订阅源

N
News and Events Feed by Topic
WordPress大学
WordPress大学
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
L
LangChain Blog
雷峰网
雷峰网
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
量子位
S
Security Affairs
T
Threat Research - Cisco Blogs
博客园_首页
云风的 BLOG
云风的 BLOG
D
Docker
AWS News Blog
AWS News Blog
腾讯CDC
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
U
Unit 42
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
I
InfoQ

博客园 - 红马天下

【转载】Rails使用gb2312/gbk与utf8共存的解决方案 【原创】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编码
红马天下 · 2008-08-13 · via 博客园 - 红马天下

 本文章所处环境为rails1.2.3,mysql 5,数据库编码使用utf8。

首先第一步请参照《【转载】Rails使用gb2312/gbk与utf8共存的解决方案

第二步,修改web前端,在action执行前对params中的中文字符进行gbk转码。主要工作在app/controller/application.rb中。

代码:

Code

第三步,修改ActionView::Helpers::InstanceTag,改变to_input_field_tag方法输出input的默认行为。这里很奇怪的是,为什么使用value_before_type_cast方法呢?完全可以使用value方法啊。看过ActiveRecord中的实现,对string和text类型,type_cast操作也是一个空操作,没有做任何改变。

代码:

Code

 第四步,修改ActiveRecord::Base和ActiveRecord::ConnectionAdapters::Column,在每次赋值和取值的时候自动进行编码转换。至于为什么这样做,请仔细研读相关代码,我也是费了好大劲才找到方案的。

代码:

Code

 第五步,修改object基类,增加gbk和utf8两个方法。 可以看到,在第四步中的使用了gbk,utf8等方法,这些都是增加到object上的方法。

代码:

Code

 这些代码放在哪里,怎么才能生效呢?我的做法是,编写一个或多个rb,放在lib中,然后在config/enviroments.rb文件的末尾使用require加载。

大功告成!