Using Ruby variable in Javascript (In App View)

Solution 1:

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

<script type="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:

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