0

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..\"]"

12
  • Have you tried to debug what is inside quick_stats_bar_chart ? Commented Sep 19, 2018 at 6:06
  • Yep, When I place a binding.pry and look at quick_stats_bar_chart[0], it's an array, just like the one I provided manually and everything worked (manually that is) Commented Sep 19, 2018 at 6:07
  • Looks like it's not valid array for javascript code. Show what is inside that variable. Commented Sep 19, 2018 at 6:08
  • What's strange though is that the other ones are arrays too and they're being accepted just fine, except quick_stats_bar_chart[1], [2], and [3] are actually array containing integers, unlike [0] which is a string. Commented Sep 19, 2018 at 6:08
  • Your errors is clear, it's syntax error, so your array isnt valid. Commented Sep 19, 2018 at 6:09

1 Answer 1

1

You need marks a string as trusted safe. It will be inserted into HTML with no additional escaping performed. Use the raw() method for his.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.