0

I’m using Rails 4.2.7. I would like to take my hash created in Rails and output a Javascript variable that contains the hash (same data associations). So I have this in my view

var my_object_names = <%=h my_object_name_hash.to_json %>;

but what is output is

var my_object_names = {&quot;1403913600000&quot;:&quot;Proudun&quot;,&quot;1437782400000&quot;:&quot;BTN Event&quot;,&quot;1466812800000&quot;:&quot;Proud&quot;}; 

This results in a Javascript error, “SyntaxError: Unexpected token &”. What is a more elegant way to take a Ruby hash and output Javascript that contains the same data?

2
  • You're escaping the output .. Commented Aug 22, 2016 at 18:59
  • What is the h after <%=? Commented Aug 22, 2016 at 18:59

1 Answer 1

1

You're asking for that value to be HTML escaped with the h function, so you're getting it that way.

The way to do it without escaping is:

<%= object.to_json.html_safe %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.