Created
April 1, 2020 23:32
-
-
Save davidmyersdev/3de2c9bcbb5d76069164441db47b7e86 to your computer and use it in GitHub Desktop.
Interpolating data into Rails views -- View segment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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