I have kind of an interesting problem.
I have a form wherein people enter information. Big surprise. If they
enter any “weird” characters like ø or é or whatever, the form will
submit and all is well. However, I have a select box for the state
which, if you’re looking at Spain, has states like A Coruña, Cádiz and
País Vasco. These are pulled from the database which is set to have
everything encoded in UTF-8. Everything we’re doing is in UTF-8.
However… when it renders the template IF someone used a non-ASCII
character in a field that appears BEFORE the select I get this error:
incompatible character encodings: ASCII-8BIT and UTF-8 (on the same
line as f.select :state)
If one of the fields AFTER the state field (like the postal code)
contains a non-ASCII character the error is reversed:
incompatible character encodings: UTF-8 and ASCII-8BIT (on the same
line as f.select :postal_code)
The more I work with encodings in Rails and Ruby in general, the more
I find myself confused and frustrated. I added config.encoding =
Encoding::UTF_8 to my application.rb, but that doesn’t appear to
affect templates at all. The problem, so far as I can see, is in one
of two places:
I either need to tell Rack to make all my string parameters encoded in
UTF-8 or I need to set my template default encoding to UTF-8. A quick
fix is:
params[:form].each { |k, v| v.force_encoding ‘UTF-8’ if v.is_a?
String }
I know this is not ideal, but I don’t understand how the view works
well enough to do this better.
What should I do to fix this problem? (Oh, and I’m using ERB, as an
FYI.)