I have a subject from an email that comes in to my rails 3 application like this :
I trying to then save this subje开发者_如何转开发ct in my database(mongodb) as a string field using mail.subject but i get error 'String not valid UTF-8' ??
Anyone know how i solve this ?
thanks alot rick
You haven't shown any sample code, but it looks like the subject returned isn't UTF-8 encoded, but your database is. Try using force_encoding
before saving the subject.
mail.subject.force_encoding("UTF-8")
EDIT:
For ruby 1.8.7, shamelessly stolen from String.force_encoding() in Ruby 1.8.7 (or Rails 2.x)
require 'iconv'
class String
def to_my_utf8
::Iconv.conv('UTF-8//IGNORE', 'UTF-8', self + ' ')[0..-2]
end
end
And then...
mail.subject.to_my_utf8
精彩评论