我正在编写一个 Rails 应用程序,并且我有一些变量通过 View 文件打印到浏览器控制台。特别是,我创建了一个具有以下格式的 ruby 哈希:
{string: [array], string: [array]}
例如,我可能拥有的一个可能的数组是
{'nil': [1,2, 3], "124": [4,5], "124/125": [6]}
当它打印到浏览器控制台时,格式已关闭。例如,现在我有:
{nil=>[1, 2, 3], "124"=>[4, 5], "124/217"=>[6]}
如何去掉添加的额外字符?
这是我的实际代码:
<% allBranches = Hash.new %>
<% currentBranch = Array.new %>
<% (1..@ancestry.length-1).each do |index| %>
<% if @ancestry[index] == @ancestry[index-1] %>
<% currentBranch.push(index) %>
<% else %>
<% allBranches[@ancestry[index-1]]=currentBranch %>
<% currentBranch = Array.new %>
<% currentBranch.push(index) %>
<% end %>
<% end %>
<script type="text/javascript">
console.log("allBranches: <%=allBranches%>");
</script>
最佳答案
使用html_safe
.
console.log("allBranches: <%=allBranches.to_s.html_safe%>");
如果allBranches
已经是一个字符串,to_s
部分是不必要和多余的。
关于javascript - 格式化控制台日志的 ruby 哈希字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15451460/