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

@active_record_or_array = Table.all

active_record_or_array = JSON.parse('<%= raw @active_record_or_array.to_json %>');

Plain text (for copy paste):

exmaple_controller.rb

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

    @active_record_or_array = Table.all
end

create.js.erb

string = '<%= @string %>';
array = <%= raw @array %>;
boolean = <%= raw @boolean %>;
integer = <%= raw @integer %>;
active_record_or_array = JSON.parse('<%= raw @active_record_or_array.to_json %>');