1

I am wondering how I can send data from my Sinatra app to Javascript.

Right now, I am able to send the data to a web page through an erb file. However, I am trying to present the data better which requires the information to go through some JS and am not sure how to do that.

<!-- language: lang-js -->

$(function() {

var data = [{
    label: "Chrome",
    data: 40
}, {
    label: "Safari",
    data: 35
}, {
    label: "Mozilla",
    data: 15
}, {
    label: "Firefox",
    data: 10
}];

var plotObj = $.plot($("#flot-pie-chart"), data, {
    series: {
        pie: {
            show: true
        }
    },
    grid: {
        hoverable: true
    },
    tooltip: true,
    tooltipOpts: {
        content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
        shifts: {
            x: 20,
            y: 0
        },
        defaultTheme: false
    }
});

});

How do I pass Ruby into this JS object and render it on our HTML?

1 Answer 1

2

You can set a variable in ruby (rubyVarName = someValue) and then to access it set var someVar = <%=rubyVarName%>;. That will allow you to access the value of rubyVarName as someVar in your JS.

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.