user4778326
user4778326

Reputation:

Force encoding on ActionController::Parameters

I'm attempting to save the params from PayPal's IPN so they can be accessed later and used for cross reference.

Simple little bit of code that saves it in the paypal_log column:

@order.paypal_log = params.to_json

Worked fine, pushed to production and still was working fine. Investigating the logs for another issue, I came across the following line:

Completed 500 Internal Server JSON::GeneratorError (source sequence is illegal/malformed utf-8):

The line it points to is the above params.to_json

After looking around I discovered force_encoding("ISO-8859-1").encode("UTF-8") (source)

I changed the line to params.force_encoding("ISO-8859-1").encode("UTF-8").to_json and ran my tests with a error of:

undefined method `force_encoding' for #<ActionController::Parameters:0x000000073357f0>

This makes me thing that for some reason, you can't parse the whole params variable, only a hash. Any possible solution for forcing encoding for all params?

Upvotes: 2

Views: 1072

Answers (1)

Mike Fahn
Mike Fahn

Reputation: 36

Can iterate over each param and force the encoding.

params.each { |k, v| params[k] = v.force_encoding('ISO-8859-1').encode('UTF-8') }

Upvotes: 2

Related Questions