If I have an assigns variable, how can I pass that to the Javascript tag? The following would work in Ruby Slim, but does not seem to in Elixir's Slime.
javascript:
window.currentUser = { username: @username }
If I have an assigns variable, how can I pass that to the Javascript tag? The following would work in Ruby Slim, but does not seem to in Elixir's Slime.
javascript:
window.currentUser = { username: @username }
You can inject a value using #{}
:
javascript:
window.currentUser = { username: #{username} }
You'll probably want to JSON encode the variable so that strings are inserted as double quoted escaped JavaScript strings. With Poison
, you can do:
javascript:
window.currentUser = { username: #{Poison.encode!(username)} }
If username
is the string foo
, the first one will render to:
<script>window.currentUser = { username: foo }</script>
while the second one will render to:
<script>window.currentUser = { username: "foo" }</script>