Escape String - Output rails string in Javascript [duplicate]
问题 This question already has answers here : How to pass Ruby variables to a JavaScript function in a Rails view? (6 answers) Closed 5 years ago . I'm trying to assign a string value to a javascript object in my .erb file like so: var data = { 'name': '<%= @product.name %>', ... }; The problem is, if the value of name is Tom's small ears , the output of data.name would be Tom's small ears . Is there a way to escape special characters? I tried doing 'name': '<%= raw @product.name %>' but Uncaught