Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby array to javascript - Rails

I am trying to pass a ruby array to a js view (js.erb format) but it doesn't work at all.

var array = "<%= @publishers_list %>";

The variable array is just set as a string with all the array's values in it.

Is there a way to keep the array format ?

Edit

I just realized it was because of my array format.

[{:label => "name1", :value => value1}, {:label => "name2", :value => value2}]

I tried to pass a simple array like:

[1,2,3]

and it worked fine.

The question is now: how can I pass this kind of array ? I really need to keep these hashes in it because I want to put it as a source of a jQuery autocomplete.

like image 592
Mencls Avatar asked Apr 25 '13 09:04

Mencls


1 Answers

var array = <%= escape_javascript @publisher_list.to_json %>
like image 73
manoj Avatar answered Nov 04 '22 20:11

manoj