zoukankan      html  css  js  c++  java
  • .erb 中不能显示从mysql检索出的中文 incompatible character encodings: UTF8 and ASCII8BIT

    问题现象:

      在.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

      
  • 相关阅读:
    [XJOI]noip43 T2多人背包
    Codeforces Round #198 (Div. 2)E题解
    [XJOI]noip40 T2统计方案
    Codeforces Round #198 (Div. 2)C,D题解
    Codeforces Round #198 (Div. 2)A,B题解
    9.19[XJOI] NOIP训练37
    9.18[XJOI] NOIP训练36
    kmp算法详解
    [模板系列] AC自动姬
    luogu1967[NOIP2013D1T3] 货车运输 (最大生成树+LCA)
  • 原文地址:https://www.cnblogs.com/kting/p/2177530.html
Copyright © 2011-2022 走看看