Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidmyersdev/3de2c9bcbb5d76069164441db47b7e86 to your computer and use it in GitHub Desktop.
Save davidmyersdev/3de2c9bcbb5d76069164441db47b7e86 to your computer and use it in GitHub Desktop.
Interpolating data into Rails views -- View segment
<!-- app/views/notifications/index.html.erb -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/he/1.2.0/he.min.js" integrity="sha256-awnFgdmMV/qmPoT6Fya+g8h/E4m0Z+UFwEHZck/HRcc=" crossorigin="anonymous"></script>
<script>
const notifications = JSON.parse(
// Rails encodes special characters for our security when interpolating strings into views
// the he library allows us to safely convert those characters back
he.decode('<%= notifications.to_json %>')
);
// do something with the notifications
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment