I am trying to pass a json object from a rails (.erb) view to javascript.
Here is what my view looks like:
<p>
<%= content_tag "div", id: "slider", data: {slider: {min: 1, max: 10}} do%>
<%end%>
</p
And my javascript:
jQuery ->
$('#slider').slider($('#slider').data('slider'))
How do I pass {min: 1, max:1} to my coffeescript file?
thanks
Check out this rails cast, and specifically the gon gem.
Also your tag is generating a div with a
dataattribute. After your page loads find your div using jquery and query for the data attribute value. Once you have that you can do whatever you want with it.If your jquery is in your erb file, then it will load before your coffeescript, so you could then just store it in a variable and have it be the return value of your function. Then you can call that function from your coffeescript file to get the value.