[Java] Use the ruby variable with javascript.

less than 1 minute read

There seem to be various methods, but this is the method I implemented this time.

Conclusion

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.

Besides, it can be implemented by using a gem called “gon”.