ruby
来源:互联网 发布:linux打开html文件 编辑:程序博客网 时间:2024/06/09 12:45
.erb 中不能显示从mysql检索出的中文 incompatible character encodings: UTF-8 and ASCII-8BIT
http://blog.163.com/xingchao_gan/blog/static/183374228201186101653701/
问题现象:
在.html.erb中显示mysql中的中文字符时会出现
incompatible character encodings: UTF-8 and ASCII-8BIT 错误
问题可能原因:
mysql_adapater出来的数据是ASCII-8BIT
解决方法:
修改根目录下的\Ruby192\lib\ruby\gems\1.9.1\gems\activerecord-3.0.9\lib\active_record\connection_adapters中的
mysql_adapter.rb文件
def select(sql, name = nil)
@connection.query_with_result = true
result = execute(sql, name)
rows = []
result.each_hash { |row| rows << row }
result.free
@connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
# add begin
if @config[:encoding] && @config[:encoding]=="utf8"
rows.each do |row|
row.each do |key, value|
if (value.class == String)
value.force_encoding("UTF-8")
end
end
end
end
# add end
rows
end
@connection.query_with_result = true
result = execute(sql, name)
rows = []
result.each_hash { |row| rows << row }
result.free
@connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
# add begin
if @config[:encoding] && @config[:encoding]=="utf8"
rows.each do |row|
row.each do |key, value|
if (value.class == String)
value.force_encoding("UTF-8")
end
end
end
end
# add end
rows
end
0 0
- ruby
- ruby
- ruby
- Ruby
- Ruby
- Ruby
- ruby
- ruby
- ruby
- Ruby
- RUBY
- ruby
- Ruby
- ruby
- Ruby
- ruby
- ruby
- Ruby
- 工信部发2016年中国互联网企业100强名单
- STM32+CC1101 低功耗2
- AppCompatActivity和Activity主题问题
- [LeetCode] 6. ZigZag Conversion
- (电工基地笔记)Vivado固化至SPI Flash
- ruby
- 剑指Offer--064-数据流中的中位数
- 为iOS项目集成CocoaPods支持
- 深入理解乐观锁与悲观锁
- Reverse String
- 数据库学习之————sqlite3数据库常用函数
- HDU2825 Wireless Password(状态压缩DP)
- hibernate获取connection连接
- Android Studio show line numbers