Javascript in my .html.erb using ruby-escaping built-in tasks

I am trying to insert the data that I defined in my controller, in my opinion. in view.html.erb:

<script>
some_var = <%= @var_data %>
some_ints = <%= @int_data %>
</script>

in my controller:

@var_data = ['hi', 'bye']
@int_data = [1,2,3,4]

however, when I look at the generated html file, it looks like

<script>
some_var = [&quot;hi&quot;, &quot;bye&quot;]
some_ints = [1,2,3,4]
</script>

those. ints are fine, but all the quotes slipped away. I tried

some_var = <%= @var_data.map {|i| i.html_safe} %>

but it did nothing (and also html_safe did not work on the whole array). How should I do it?

thank

+3
source share
1 answer

Have you tried this?

<%=raw @var_data %>
+7
source

Source: https://habr.com/ru/post/1789783/


All Articles