# pass ruby array to javascript function

**URL:** <https://discuss.rubyonrails.org/t/pass-ruby-array-to-javascript-function/38496>\
**Category:** rubyonrails-talk\
**Created:** [January 19, 2009, 9:00pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-to-javascript-function/38496 "2009-01-19T21:00:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_L](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@Dave\_L](https://discuss.rubyonrails.org/u/Dave_L)\
**Post date:** [January 19, 2009, 9:00pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-to-javascript-function/38496/1 "2009-01-19T21:00:21Z")

</div>

Im wondering how to go about passing a ruby array to a javascript function. I was able to do this the other way around and pass a javascript array to an action using JSON and convert it back to a ruby array:

Object.toJSON(\<%= array\_or\_string\_for\_javascript(@array) %\>

then in the action I was able to do:

ruby\_array = JSON.parse(params[:array])

But this time I want to pass an array to a javascript function through a function call within :update

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :update do |page| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page \<\< "setTime(#{time})" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

where setTime is the javascript function and time is the ruby array. I can pass a number and it works fine, but with strings and arrays I get an error. How would I go about passing a ruby array to a javascript function in this context? Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Dave\_L](https://avatars.discourse-cdn.com/v4/letter/d/7feea3/32.png) [@Dave\_L](https://discuss.rubyonrails.org/u/Dave_L)\
**Post date:** [January 19, 2009, 9:11pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-to-javascript-function/38496/2 "2009-01-19T21:11:28Z")

</div>

Nevermind, I figured it out: array.to\_json works just fine. Way simpler than I thought.
