I will describe the problem. I want to connect javascript to display information. The view itself:
<script src= "https://cdn.zingchart.com/zingchart.min.js"></script> <h2> Топливная статистика байка <%= @bike.name %>, владелец <%= @bike.user_name %> </h2> <% get_stata_l_na_100_km %> //*получаю значение json из хелпера <div id='chartDivRashod'> <%= javascript_include_tag "fuel_chart" %> //*подгружаю JS-график из файла </div> in the helper I am getting the @ json_stata_l_na_100_km variable in json-format, the helper itself:
module StataFuelsHelper def get_stata_l_na_100_km @stata_graph = [] @view_stata_fuel.each do |stata| @stata_graph << (stata.refueling / (stata.odo_delta / 100)).round(2).to_f end @json_stata_l_na_100_km = @stata_graph.to_json end end and the loadable javascript file itself fuel_chart.js with the ZingChart graph:
var chartRashod = { type: "bar", // Specify your chart type here. "plot": { "value-box": { "text": "%node-value" } }, title: { text: "средний расход, л/100 км" // Adds a title to your chart }, legend: { "header": { "text": "байк" } }, // Creates an interactive legend "type": "bar", "scale-x": { "zooming": true }, "scale-y": { "zooming": false }, "preview": { "visible": true }, series: [ { text: "1", values: <%= JSON.parse(@json_stata_l_na_100_km) %> } ] }; zingchart.render({ // Render Method[3] id: "chartDivRashod", data: chartRashod, height: 400, width: 600 }); At startup, the console throws a script error SyntaxError: expected expression, got '<' values: <% = JSON.parse (@ json_stata_l_na_100_km)%>}. If you remove the <% =%> wrapper, it produces a SyntaxError: illegal character error, i.e. does not accept the string. But if I can not stand javascript code in a separate file and write it in the view, wrapped in a script ... / script - then the schedule works out with a bang without problems.
How to solve the problem, what am I doing wrong? Thank you in advance.
fuel_chart.jswhere is the project? - D-side