如何在rails部分中正确转义javascript标记?

时间:2021-10-18 00:06:40

my file is _file.html.erb :

我的文件是_file.html.erb:

<script type="text/javascript">
  $(function(){
    console.log("<%= escape_javascript( list ) %>");
  });
</script>

This fails. But if I put in a debugger before the javascript gets rendered, and run list, it returns properly in console. How can I get an active working JSON object from list so that I can work with it?

这失败了。但是如果我在javascript被渲染之前放入调试器并运行list,它会在控制台中正确返回。如何从列表中获取活动的JSON对象,以便我可以使用它?

1 个解决方案

#1


1  

Why not this?

为什么不呢?

<script type="text/javascript">
  $(function(){
    var list = <%= list.attributes.to_json %>;
    console.log(list);
  });
</script>

#1


1  

Why not this?

为什么不呢?

<script type="text/javascript">
  $(function(){
    var list = <%= list.attributes.to_json %>;
    console.log(list);
  });
</script>