Reputation: 21
I want to pass hash from my controller to JS array but it can work. These are what I do: In controller:
@tag_cloud = []
@tag_cloud[0] = {}
@tag_cloud[0]["text"]="Lorem"
@tag_cloud[0]["weight"]=15
.....
In view:
var word_list =<%=@tag_cloud.to_json%>
$(function() {
$("#my_tag_cloud").jQCloud(word_list);
});
I don't understand why json can't load to word_list
Upvotes: 2
Views: 710
Reputation: 2185
As I could not find a minimal working rails version elsewhere:
I assume you have followed the installation instructions.
In order for the JQCloud rails gem to do its magic, a convenient format is an array of hashes. Normally we build this in the model to keep the controller slim, but for the sake of showing a minimal demo, you can put the following definition into a controller action:
@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}
]
Now, in your corresponding view, add
<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>
... and you should see the word cloud.
Upvotes: 0
Reputation: 616
Use
var word_list =<%= raw @tag_cloud.to_json%>
to prevent escaping quotes as "
Upvotes: 1
Reputation: 249
jQCloud works with downcase attributes. For example "text","link" , but not "Text","Link" and so on.
Upvotes: 1