简体   繁体   English

如何正确在Rails部分中转义javascript标签?

[英]How can I properly escape javascript tags in a rails partial?

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. 但是,如果我在呈现JavaScript之前放入debugger ,然后运行list ,它将在控制台中正确返回。 How can I get an active working JSON object from list so that I can work with it? 如何从list获取活动的工作JSON对象,以便可以使用它?

Why not this? 为什么不呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM