简体   繁体   中英

error: invalid byte sequence in UTF-8 after upgrading ruby 1.8.7 to ruby 1.9.2

I had my app running in ruby 1.8.7 and rails 3.0.11 ,I upgraded it with 1.9.2 ruby and rails 3.2.2. it has a utf convertor like this

@utf8_converter = Iconv.new('UTF-8//IGNORE', 'UTF-8')
......  
......
def utf8(untrusted_string)
valid_string = @utf8_converter.iconv(untrusted_string + ' ')[0..-2]
return valid_string

Unto my Understanding Iconv doesn't support ruby 1.9.2. how can make it run?

Thanks

I believe this is should get you on the right track:

def utf8(untrusted_string) 
  valid_string = (untrusted_string + ' ').encode('utf-8')
  return valid_string
end

The @utf8_converter variable is no longer needed as Iconv is deprecated, so you can get away with just your utf8 method.

Ruby 1.9.2 does support Iconv , if you are using ruby through rvm, you should install it in the following way,

$ rvm pkg install iconv
$ rvm reinstall 1.9.2 --with-iconv-dir=$rvm_path/usr

Read more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM