butterywombat
butterywombat

Reputation: 2089

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

Upvotes: 3

Views: 1298

Answers (1)

Mauricio
Mauricio

Reputation: 5853

have you tried this?

<%=raw @var_data %>

Upvotes: 7

Related Questions