[Chartjs]-Get hash value Rails / Chart.JS

1👍

Instead adding a loop in your JS, initialize the polls variable as an array of ids, this way you avoid having to collect them, and adding Ruby logic in the JS code:

@polls4 = @polls.where('question_id = ?', 4).pluck(:nom)

As you’re grouping you get that hash of key/value data, using pluck you get just the id of each Poll:

So the chart:

var answer = "<%= @polls4 %>";
var myChart = new Chart(ctx, { 
  ...
});

I’d recommend you to rename the @polls4 variable to something more descriptive and avoiding using numbers in variable names.

Leave a comment