Handling Non-UTF-8 Params In Rails
Today I woke up to the following 500 error notification from mindfulchoices:
Seemingly, a SEO bot in New York had submitted a message via the ‘Contact Us’ form, though it included a non-UTF-8 sequence:
After a little reading around the subject, it seems Rails tries everything it can to force browsers to encode their submissions as UTF-8.
What can you do if/when a client refuses to conform and sends non-unicode characters to your server? Surprisingly, I couldn’t find an idiomatic solution to this issue.
A post from the all-knowing thoughtbot switched me on to the String#encode
method.
Since my code was already using Rails Strong Params, I bolted some extra code to the end of the method, stripping out any non-UTF-8 characters:
I’m curious, do you know of a better (more “Rails-y”) way to mitigate this problem?