2

我想将哈希从我的控制器传递到 JS 数组,但它可以工作。这些是我所做的: 在控制器中:

@tag_cloud = []    
@tag_cloud[0] = {}
@tag_cloud[0]["text"]="Lorem"
@tag_cloud[0]["weight"]=15
.....     

鉴于:

 var word_list =<%=@tag_cloud.to_json%>
 $(function() {
   $("#my_tag_cloud").jQCloud(word_list);
 }); 

我不明白为什么 json 无法加载到word_list

4

3 回答 3

1

利用

var word_list =<%= raw @tag_cloud.to_json%>

防止将引号转义为&quot;

于 2014-03-06T14:10:12.213 回答
1

jQCloud 使用小写属性。例如 "text","link" ,但不是 "Text","Link" 等等。

于 2012-07-17T13:59:18.620 回答
0

因为我在其他地方找不到最小的工作导轨版本:

我假设您已按照安装说明进行操作。

为了让 JQCloud rails gem 发挥它的魔力,一种方便的格式是哈希数组。通常我们在模型中构建它以保持控制器苗条,但为了展示一个最小的演示,您可以将以下定义放入控制器动作中:

@tag_cloud = [
   { text: "test", weight: 15},
   { text: "Ipsum", weight: 9, link: "http://jquery.com/"},
   { text: "Dolor", weight: 6, html: {title: "I can haz any html attribute"}},
   { text: "Sit", weight: 7},  {text: "Amet", weight: 5}
]

现在,在您相应的视图中,添加

<script type="text/javascript">
  var word_array =  <%= raw @tag_cloud.to_json %> ;
  $(function() {
    // When DOM is ready, select the container element and call
    // the jQCloud method, passing the array of words as the first argument.
    $("#example").jQCloud(word_array);
  });
</script>

<div id="example" style="width: 550px; height: 350px;"></div>

...你应该看到词云。

于 2015-11-20T11:30:02.517 回答