title: How to deal with HTML entities in Rails to_json output?
body: <p>I'm writing an app that uses Rails on the backend and javascript/backbone on the frontend.  I'm trying to <a href="http://backbonejs.org/#Collection-reset" rel="nofollow noreferrer">bootstrap some rails models into my javascript</a>. Specifically, I'd like to load the contents of <code>@courses</code> into a js variable called <code>window.courses</code>. I've got the following in an <code>html.erb</code> file.</p>
  
  <pre><code>&lt;%= javascript_tag do %&gt;
      window.courses = JSON.parse('&lt;%= @courses.to_json %&gt;');
  &lt;% end %&gt;
  </code></pre>
  
  <p>I'm expecting the <code>erb</code> preprocessor to render this into valid javascript, like so</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  //&lt;![CDATA[
      window.courses = JSON.parse('[{"code":"myCourseCode", ...
  //]]&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>... but, instead, I'm getting code that includes <a href="http://www.w3schools.com/html/html_entities.asp" rel="nofollow noreferrer">HTML entities</a>.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  //&lt;![CDATA[
      window.courses = JSON.parse('[{&amp;quot;code&amp;quot;:&amp;quot;myCourseCode&amp;quot;, ...
  //]]&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>Obviously, I get javascript errors when I try to parse this.</p>
  
  <p>Does anyone know how I can deal with these HTML entities in order to produce valid javascript?  I realize that one option would be to <a href="https://stackoverflow.com/a/1912546/765287">unescape the entities on the client side</a>, but this seems like a roundabout solution.  Is there a way that I can get Rails to produce JSON that doesn't need unescaping?</p>