Skip to content Skip to sidebar Skip to footer

Using Ruby Variable In Javascript (in App View)

Currently, I have a ruby variable accessible by the view called @json (which contains information I need in JSON format) However, I want to pass this into a script area such as &l

Solution 1:

Assuming the script tag you mentioned is in a html erb view you can just use this:

<scripttype="text/javascript"charset="utf-8">var json = <%= @json || 'null' %>;
</script>

Solution 2:

Another way to do it is like this: var json = "#{ @json || 'null' }"

Solution 3:

Better wrap that in quotes:

<scripttype="text/javascript">var json = "<%= @json %>";
</script>

Post a Comment for "Using Ruby Variable In Javascript (in App View)"