上下文:
我正在尝试使用 jqPlot 创建饼图,从 Rails 数据库收集数据。
出现问题是因为 jqplot 不理解从 event.name 生成的字符串。如果我手动插入几个字符串,而不通过 Rails,它工作得很好。n我相信它与转义不应该的字符有关。
我从 Firebug 收到的错误显示 events = [[“Title”, 1234566]]
。因此,jqplot 尝试使用 “Title”
作为键来创建此图表。它不喜欢这样。
我尝试过使用 html_safe,但没有成功。
代码:
<% things = [] %>
<% @topEvents.each do |event| %>
<% things << [event.name, event.total] %>
<% end %>
$.jqplot('topEvents', [ <%= things %> ], {
seriesDefaults : {
renderer : jQuery.jqplot.PieRenderer,
rendererOptions : {
showDataLabels: true
}
}
});
环境:
- ruby 1.9.3
- Rails 3.2.6
- SQLite3 3.6.20
- jquery-rails 2.0.2 (RubyGem)
- JQuery 1.7.2
- jqplot 1.0.0_r1012。
最佳答案
对于 future 的 Google 员工...
虽然"string"
和'string'
一旦它们在 Ruby 中声明,可能是等价的,但这个事实对于 javascript 来说是无关紧要的。在<script>
内标签,Ruby 字符串将显示为双引号。如果您想要单引号,则必须将字符串作为字符串以外的其他内容导入,然后稍后手动添加引号。
Javascript会理解'<%= event.total %>'
正好。它将使用 Rails 打印出总数,然后脚本将在两侧添加单引号。
希望这对任何人都有帮助!
关于jquery - 将 Ruby 合并到 jqPlot 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11742196/