Use chartjs with rails

1.1k views Asked by At

I'd like to use chartjs with rails in a view, it works great, but when using an option like this one :

legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"

rails will interpret <% and run name.toLowerCase() as a ruby code, which is actually javascript, how can I escape '<%' in rails?

1

There are 1 answers

3
Max Williams On BEST ANSWER

add an extra percent sign:

<%%= @foo %> =>  "<%= @foo %>"

or

<%% foo %> => "<% foo %>"

BTW in case you want to start googling for stuff instead of using SO, i googled "escape erb tags" to get this, i'd not done it before.