2

I am using Rails 3.2.3 and Ruby 1.9.3. I have a model called called Post that accepts a title and a description. The front-end of the site receives information submitted through the back-end through an ajax request. When I fill out the form with, let's say

title: foo
content: foobar

and submit it, I am able to view data through the front-end without a problem.

However, whenever I submit non-utf8 data through the form, for example (mind the fancy quotes):

title: foo
content: “foobar” 

When I try to render the form I get the following error:

ActionView::Template::Error (incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string))

My .js.erb file looks like this:

$("#my_post").html('<%= escape_javascript(render :partial => 'post') %>');

I realize this is an issue with encoding, but I'm not sure how I should handle it the best way. I thought of several options:

  1. Strip out non-utf8 by using the iconv library -- do this via a before_save filter for every single model in my application
  2. Specifying at the top of the js that the document contains utf-8 (not sure this would work)
  3. Using accept-charset="UTF-8" in my form to force the browser to avoid submission of non-utf-8 content.

I'm not even sure these solutions would help and the most efficient way to do this would be. Thanks!

Community
  • 1
  • 1
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175

2 Answers2

0

You need to look carefully to see if

  • You're actually sending non-UTF-8 data to your app, or
  • You're sending UTF-8 data, but it is not being recognized by Ruby/Rails

To see which it is, you need to examine the data on the "wire." (What's being sent on the Internet.) Use a peeking tool such as Wireshark or a proxy spy such as Fiddler

Curly quotes can be sent using 8859 or UTF-8.

Recommendation You should set the HTML page to be UTF-8. Any Ajax sent from scripts on the page should then also use UTF-8. See http://www.utf8.com/

Added (Re: comment about how Rails sets form's character encoding)

The issue for Ajax character encoding is how was the page's encoding set. A blog post. So be sure to set the page's UTF-8 encoding in your page template.

Larry K
  • 47,808
  • 15
  • 87
  • 140
  • Rails sets the charset to UTF-8 by default. – Gabe Kopley May 29 '12 at 02:56
  • @GabeKopley -- That's only setting the encoding for the form. It has no effect on Ajax calls. – Larry K May 29 '12 at 13:56
  • The linked blog post author is wanting his page in the ISO-8859-15 charset. You don't need to do this for UTF-8, it's the Rails default. – Gabe Kopley May 29 '12 at 16:26
  • How to let users post an arbitrary encoding and the Rails server detects and convert it to UTF-8? My standard form has accept-charset but the browser never converts the uploaded files (non UTF-8) to UTF-8 – hammady Nov 24 '13 at 11:41
0

I suspect that you're not using the form helpers because you mention the question of adding accept-charset="UTF-8" to your form.

The form helpers will add the accept-charset attribute as well as the snowman parameter which together should ensure you get UTF-8 data from the browser.

See the Rails Form helpers guide.

Community
  • 1
  • 1
Gabe Kopley
  • 16,281
  • 5
  • 47
  • 60