如何转换“to double qoutes

时间:2016-02-25 18:43:55

标签: javascript jquery ruby-on-rails ruby

我有一个ruby json对象。例如

在控制器中

@my_hash = {"5":{"id":5,"name":"ANKLOS","code":"724.9","description":"Ankylosis of spine (Spine Fusion)","title":"ANKLOS - 724.9 - Ankylosis of spine (Spine Fusion)"},"3":{"id":3,"name":"Loren Lipsum","code":"Test","description":"Test t2 stest","title":"Loren Lipsum - Test - Test t2 stest"}}

我在视野中使用了 @my_hash 。我的内容来自表格。

<script type="text/javascript">
    var my_hash = <%= @my_hash.to_json %>;
</script>

但是my_hash的结果就像

var diagnosisCodes = {&quot;&quot;:{&quot;code&quot;:&quot;\u0026larr;&quot;,&quot;description&quot;:&quot;&quot;},&quot;5&quot;:{&quot;id&quot;:5,&quot;name&quot;:&quot;ANKLOS&quot;,&quot;code&quot;:&quot;724.9&quot;,&quot;description&quot;:&quot;Ankylosis of spine (Spine Fusion)&quot;,&quot;title&quot;:&quot;ANKLOS - 724.9 - Ankylosis of spine (Spine Fusion)&quot;},&quot;3&quot;:{&quot;id&quot;:3,&quot;name&quot;:&quot;Loren Lipsum&quot;,&quot;code&quot;:&quot;Test&quot;,&quot;description&quot;:&quot;Test t2 stest&quot;,&quot;title&quot;:&quot;Loren Lipsum - Test - Test t2 stest&quot;}};

我需要像@my_hash一样的输出。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

使用

@my_hash.to_json.html_safe

如上所述: Handling JSON in JS/ERB template in Rails 3