Search code examples
javascriptruby-on-railsujs

Rail UJS with json


In my javascript response to a form that was submitted with data-remote=true, I have this line:

my_function("<%[email protected]_json%>")

But the arg passed to my_function is filled with &quot;, which my javascript fails to interpret as an object. I assume I need to escape <%[email protected]_json%> somehow, but I don't know which javascript function to use. Any ideas?


Solution

  • By default everything you write to template using <%= %> is escaped. To prevent Rails from doing that you can use raw helper:

    my_function("<%= raw @object.to_json %>")