Use ruby array for a javascript array in erb. Escaping quotes

走远了吗. 提交于 2019-12-29 07:37:32

问题


i've found numerous things online for this but they dont work for me. am i missing something.

In my controller i have

@t = ["a","b","c"]

in the erb file that is 'callback' the @t renders like so:

["a", "b", "c"] 

i've done hacks to replace the " to proper ' symbols. I've read that to_json should work but it doesnt. The following does not work ["a","b","c"].to_json. The results are the same.


回答1:


to_json is working fine. What you're running into is Rails 3.x's XSS protection. There's a good article on this at Railscasts/ASCIIcasts. The gist, though, is that you need to use the raw or html_safe methods:

In your controller:

@t_json = @t.to_json.html_safe

OR in your view:

<%= raw @t %>


来源:https://stackoverflow.com/questions/7561430/use-ruby-array-for-a-javascript-array-in-erb-escaping-quotes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!