There seem to be various methods, but the method I used this time.
index.html.erb
@test = { 'hoge' => 'huga', 'hogehoge' => 'hugahuga' }
<div id="hoge" data-hoge-id="<%= @test.to_json %>"></div>
<script>
var test = $('#hoge').data('hoge-id');
</script>
That was all.
It can also be implemented using a gem called "gon".
Recommended Posts