Bill
Bill

Reputation: 545

render json Encoding::UndefinedConversionError ("\xC3" from ASCII-8BIT to UTF-8)

Rails 4.1.4 app with Ruby 2.3.0

had to unexpectedly move an app to a different host. I've been battling this error for a couple of days and not sure why it's started at the new server. I get the error at this line of code

render json: { :result => cust_setup }, status: 200

cust_setup is data returned from a MySQL db. Changing the Encodeing to UTF8 didn't help. Tried some different ways to output that with the same result. Not sure what to do with this.

Edit: One thing I found during my searching here is that we moved from the mysql2 to the mysql gem as we couldn't get the mysql2 gem to work. the mysql gem seems to be the culprit of changing things to ASCII-8BIT.

Edit 2: Coupled with the above, there is 1 field in this table that has Spanish language in it. If I remove that, the error disappears. So, it's the special characters in the spanish text coupled with moving to the mysql gem. I'm not sure what I need to do to resolve this at the moment.

Edit 3: i tried going back to mysql2 gem, got it running, still throws the same error.

Edit 4: added a getter in the model for the specific field. encoding works in the model if i specify it on the a specific field. i have this now:

self[:sp_legal].force_encoding("UTF-8").encode("ASCII-8BIT")

which throws this error

Encoding::InvalidByteSequenceError ("\xF3" followed by "n" on UTF-8):

Edit 5: I have it working for now. I don't like the way I had to do it, it's removing characters it doesn't know what to do with, so not sure what i'll get at times, but it's working. here is my final getter

self[:sp_legal].force_encoding("UTF-8").encode("ASCII-8BIT", invalid: :replace, undef: :replace)

Upvotes: 0

Views: 2711

Answers (1)

Bill
Bill

Reputation: 545

I resolved this, sorta... see below (from OP)

Edit 4: added a getter in the model for the specific field. encoding works in the model if i specify it on the a specific field. i have this now:

self[:sp_legal].force_encoding("UTF-8").encode("ASCII-8BIT")

which throws this error

Encoding::InvalidByteSequenceError ("\xF3" followed by "n" on UTF-8):

Edit 5: I have it working for now. I don't like the way I had to do it, it's removing characters it doesn't know what to do with, so not sure what i'll get at times, but it's working. here is my final getter

self[:sp_legal].force_encoding("UTF-8").encode("ASCII-8BIT", invalid: :replace, undef: :replace)

Upvotes: 0

Related Questions