Passing instance variables from controller to javascript inject

Popular in everyday use for getting backend values into html. Screenshots bellow is self explanatory. Only string requires different syntax than other types. || '' and || [] is written intentionally to show that nil's should be avoided. Otherwise crash is guaranteed.

Passing instance variables from controller to javascript inject 2021-05-24


Plain text (for copy paste):

exmaple_controller.rb

def create
    @string = string || ''
    @array = array || []
    @boolean = boolean
    @integer = integer
end

create.js.erb

string = '<%= @string %>';
array = <%= raw @array %>;
boolean = <%= raw @boolean %>;
integer = <%= raw @integer %>;