I am trying to pass an array back from a helper, and while it works for some part of my javascript, it doesn't work for the other half. For example, here's my helper:
def quick_stats_bar_chart
names = []
emails = []
students = []
results = []
@exams.each do |exam|
names << trim_exam_name(exam.name)
emails << exam.exam_results
student << exam.exam_results
results << exam.exam_results
end
return [names, emails, students, results]
end
and here's my view in the javascript
<script>
$(function(){
var barData = {
labels: ["Random"],
datasets: [
{
label: "Emails",
backgroundColor: '#dedede',
pointBorderColor: "#dedede",
data: <%= quick_stats_bar_chart[1] %>
},
{
label: "Students",
backgroundColor: '#a3e1d4',
pointBackgroundColor: "#a3e1d4",
pointBorderColor: "#a3e1d4",
data: <%= quick_stats_bar_chart[2] %>
},
{
label: "Results",
backgroundColor: '#b5b8cf',
pointBackgroundColor: "#b5b8cf",
pointBorderColor: "#b5b8cf",
data: <%= quick_stats_bar_chart[3] %>
}
]
};
var barOptions = {
responsive: true
};
var ctx2 = document.getElementById("participationChart1").getContext("2d");
new Chart(ctx2, {type: 'bar', data: barData, options:barOptions});
});
The code labels: ["Random"] is where I have issues. If I leave it like this, it works, but I cannot use <%= quick_stats_bar_chart[0] %> because I get an error stating
SyntaxError: expected expression, got '&'
The labels option is supposed to take an array, so I'm not sure why the array isn't being passed and parsed correctly.
Any thoughts? Again, I can manually put in an array, but trying to call it from the helper gives me this error.
Edit
Here's what quick_stats_bar_chart[0] looks like:
[2] pry(#<#<Class:0x00007f0c99675370>>)> quick_stats_bar_chart[0]
=> ["Random.."]
[3] pry(#<#<Class:0x00007f0c99675370>>)> quick_stats_bar_chart[1]
=> [1]
It gives me an error on [0] but not [1].
Here's the FULL variable output:
[1] pry(#<#<Class:0x00007f0c99675370>>)> quick_stats_bar_chart
=> [["Random.."], [1], [1], [0]]
Here's with raw
[2] pry(#<#>)> raw(quick_stats_bar_chart[0])
=> "[\"Random..\"]"
quick_stats_bar_chart?binding.pryand look at quick_stats_bar_chart[0], it's an array, just like the one I provided manually and everything worked (manually that is)