开发者

Ruby encoding question

I'm saving scraped data to a web app, and here's a sample param:

400\xB0F.

This is the 'degree' character from a website, but when I put that into my model I get the dreaded invalid byte sequence in UTF-8 error.

Since it's coming from the web I thought I might try some client side encoding, so javascript turns that into: 400%B0F. This can at least get saved by ActiveRecord with no issue, but Rails seems to be escaping it again on the way out so those entities aren't 开发者_C百科decoded by the browser, so my show method shows the entire encoded string.

Where should I be cleaning up my input data, and what methods might be the best to use for unpredictable input?

Thanks!


Years ago I had, and solved, this very same problem in builder. Take a look at the to_xs method: http://builder.rubyforge.org/classes/String.html#M000007

You can require builder, and use it directly (you might want to pass false to escaping or you will get entity escaped output). Either that, or simply steal and adapt the source.

Update: here is the original, standalone, library: http://intertwingly.net/stories/2005/09/28/xchar.rb


Perhaps you can use a binary form (like for upload file) with enctype="multipart/form-data" in form tag. Like this, you can use this data as a binary data ?

It's depends perhaps of waht you do with this data.


URI.unescape was the trick, after I encoded it client-side

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜