0

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 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!

2
  • It looks like the website is encoded with CP-1252. I'm not sure how you'd go about decoding this though, sorry. Commented Aug 5, 2011 at 5:13
  • I would look up how to use the String.encode method to effect conversions among encodings if I were you. Commented Aug 5, 2011 at 5:56

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow, an answer from -the- Sam Ruby. I'm all a-nerd-flutter! ;-)
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.