user3456553
user3456553

Reputation: 35

Send Ruby Data to Javascript in a Sinatra App

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?

Upvotes: 1

Views: 878

Answers (1)

thesecretmaster
thesecretmaster

Reputation: 1984

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.

Upvotes: 2

Related Questions