I've looked around and read a lot, but can't seem to understand how it works.
I have a bunch of variables in my Controller:
@category1 = @posts.where(category: "1")
@category2 = @posts.where(category: "2")
@category3 = @posts.where(category: "3")
Which i need to display on a chart. (@category1.size e.g.).
The Javascript of the chart accept's this parameters:
{ title: "Category 1", value: 22, color: "#2C3E50" },
{ title: "Category 2", value: 80, color: "#FC4349" },
{ title: "Category 3", value: 70, color: "#6DBCDB" },
I got this working by doing so:
<div id="metrics" data-category1="<%= @category1.size %>">
</div>
and in my JS File :
var $category1 = $("#metrics").data("category1")
and then using the variable created across the File. But i don't think that's a good Practice.
Isn't there a cleaner and Wiser way of achieving this ??
{ title: "Category 2", value: <%= @category2.count %>, color: "#FC4349" },@cateogory1& 2 & 3 are shared with this partial view