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

前端 未结 1 665
Happy的楠姐
Happy的楠姐 2020-12-19 04:08

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\"]
         


        
1条回答
  •  Happy的楠姐
    2020-12-19 04:17

    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 %>
    

    0 讨论(0)
提交回复
热议问题