Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From Ruby array to JS array in Rails- 'quote'?

I have a Ruby array like this in my controller:

 @location_list = [
        ['Mushrooms', 3],
        ['Onions', 1],
        ['Olives', 1], 
        ['Zucchini', 1],
        ['Pepperoni', 2]
      ]

And I am catching it like this in my view:

location_list = "<%= @location_list.to_json %>";

But if I do alert(location_list), I get:

[[&quot;Mushrooms&quot;,3],[&quot;Onions&quot;,1],[&quot;Olives&quot;,1],[&quot;Zucchini&quot;,1],[&quot;Pepperoni&quot;,2]]

How do I get the correspondent object without those &quot?

like image 424
Hommer Smith Avatar asked Jun 06 '12 22:06

Hommer Smith


1 Answers

Try:

<%= raw @location_list.as_json %>

Using to_json will end up rendering a string, with embedded double-quotes, and would need to be JS-escaped. And it would be a string, not an array.

like image 187
Dave Newton Avatar answered Oct 13 '22 22:10

Dave Newton