title: Embedding JSON in a Rails 4 JS/ERB template
body: <p>I'm trying to embed model data in some javascript in my RoR 4 app. The controller is generating JSON for the model data like so...</p>
  
  <pre><code>def my_controller_method
    @person = Person.find(params[:id])
    @person_json = @person.to_json(only: [:name, :id])
  end
  </code></pre>
  
  <p>and I'd like to use that json in my unobtrusive javascript to create javascript objects using JSON.parse()...</p>
  
  <pre><code>var personJSON = &lt;%= j @person_json %&gt;;
  var person = JSON.parse(personJSON);
  </code></pre>
  
  <p>but the javascript that's generated is...</p>
  
  <pre><code>var personJSON = {\&amp;quot;id\&amp;quot;:1,\&amp;quot;name\&amp;quot;:\&amp;quot;fred\&amp;quot;};
  var person = JSON.parse(personJSON);
  </code></pre>
  
  <p>and the javascript is failing silently. </p>
  
  <p>When searching for a solution, I found <a href="https://stackoverflow.com/questions/3757457/handling-json-in-js-erb-template-in-rails-3">this question asked on SO</a>, but when I try to use the html_safe method, my rails app crashes saying html_safe is an unknown method.</p>
  
  <p>Thanks in advance for your help!</p>