# Creating Javascript array from RAILS model data

**URL:** <https://www.ruby-forum.com/t/creating-javascript-array-from-rails-model-data/117252>\
**Category:** Rails\
**Created:** [October 15, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/creating-javascript-array-from-rails-model-data/117252 "2007-10-15T12:23:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Jones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@Ed\_Jones](https://www.ruby-forum.com/u/Ed_Jones)\
**Post date:** [October 15, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/creating-javascript-array-from-rails-model-data/117252/1 "2007-10-15T12:23:59Z")

</div>

Hi and thanks!

I think this should be simple: I need to initialize and then ajax  
update a Javascript array; taking the data from a rails model. I wrote  
an iterator that created a string–which looked just like my working  
test array, but then that didn’t seem to work out for writing it to  
the page.

How is this done?  
Thanks,  
Ed

---

<div class="post-metadata">

**Author:** ![Ed\_Jones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@Ed\_Jones](https://www.ruby-forum.com/u/Ed_Jones)\
**Post date:** [October 15, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/creating-javascript-array-from-rails-model-data/117252/2 "2007-10-15T13:50:37Z")

</div>

Hi Ed,  
Definitely give JSON a look if you haven’t already. That’s pretty much  
what it’s made for.  
In the action that your JS is calling, use this render option:

render :json =\> your\_data.to\_json

Or use that in a respond\_to block.

There’s also a little bit on returning JSON here:  
[http://api.rubyonrails.com/classes/ActionController/Base.html](http://api.rubyonrails.com/classes/ActionController/Base.html)

- Doug
