Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript in my .html.erb using embedded ruby--escaping problems

I'm trying to embed data I have defined in my controller in my view. 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 view the generated html file, it looks like

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

ie the ints are fine but all the quotes got escaped. I tried

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

instead but it didn't do anything (and also html_safe didn't work on the whole array). How should I do this?

Thanks

like image 546
butterywombat Avatar asked Mar 22 '26 16:03

butterywombat


1 Answers

have you tried this?

<%=raw @var_data %>
like image 100
Mauricio Avatar answered Mar 25 '26 05:03

Mauricio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!