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...
def my_controller_method
@person = Person.find(params[:id])
@person_json = @person.to_json(only: [:name, :id])
end
and I'd like to use that json in my unobtrusive javascript to create javascript objects using JSON.parse()...
var personJSON = <%= j @person_json %>;
var person = JSON.parse(personJSON);
but the javascript that's generated is...
var personJSON = {\"id\":1,\"name\":\"fred\"};
var person = JSON.parse(personJSON);
and the javascript is failing silently.
When searching for a solution, I found this question asked on SO, but when I try to use the html_safe method, my rails app crashes saying html_safe is an unknown method.
Thanks in advance for your help!