# Pass ruby array with Ajax.Request

**URL:** <https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351>\
**Category:** rubyonrails-talk\
**Created:** [January 15, 2009, 1:05pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351 "2009-01-15T13:05:23Z")\
**Posts on this page:** 7\
**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 15, 2009, 1:05pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351/1 "2009-01-15T13:05:23Z")

</div>

I am hoping someone can please help me with this. I am trying to pass an array using Ajax.Request and cannot get it working. This is what I have so far:

@data is a ruby array

new Ajax.Request( '/users/test', { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters: { data: Object.toJSON(\<%= array\_or\_string\_for\_javascript(@data) %\>)}, asynchronous:true }); })

Does anyone have any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![Brandon\_Keepers1](https://avatars.discourse-cdn.com/v4/letter/b/90ced4/32.png) [@Brandon\_Keepers1](https://discuss.rubyonrails.org/u/Brandon_Keepers1)\
**Post date:** [January 15, 2009, 1:44pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351/2 "2009-01-15T13:44:14Z")

</div>

new Ajax.Request( '/users/test', { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameters: { data: \<%= @data.to\_json %\>}, asynchronous:true }); })

Rails adds a #to\_json method to all of the built-in classes.

-- Brandon

---

<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 15, 2009, 11:56pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351/3 "2009-01-15T23:56:38Z")

</div>

I tried this, but I still get an error:

undefined method `toJSON' for :Array

---

<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 15, 2009, 11:59pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351/4 "2009-01-15T23:59:44Z")

</div>

Nevermind, Its seems to be working now. This is what shows up in the console:

"data"=\>"[object Object]"

and I am wondering, do I have to convert this json array back to ruby to use it and if so how do I do that?

---

<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 16, 2009, 12:18am UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351/5 "2009-01-16T00:18:38Z")

</div>

It seems like there is a better way to pass an array with Ajax.request into a controller and have it interpreted into a ruby array. Using link\_to\_remote, you are able to pass an array using :with -\>

\<%= link\_to\_remote 'Click me', :url =\> {:action =\> "array"}, :with =\> "'data='+test()", :update =\> 'testing'%\>

Im wondering, isnt there a way to do this using Ajax.Request?

---

<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 16, 2009, 3:14am UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351/6 "2009-01-16T03:14:46Z")

</div>

So Ive decided it might be better to take a step backwards and explain what I am trying to do to because there may be a better way of doing it. I have an array of hashes and each hash correlates to an active record object. Now what I need to do is pass this array to a controller action using Ajax.Request somehow. I could really use some help, Ive tried many different ways but none have worked! Thanks.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Olliver](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Jeremy\_Olliver](https://discuss.rubyonrails.org/u/Jeremy_Olliver)\
**Post date:** [January 16, 2009, 11:02pm UTC](https://discuss.rubyonrails.org/t/pass-ruby-array-with-ajax-request/38351/7 "2009-01-16T23:02:51Z")

</div>

Hi David,

That depends on what you need the AR objects for, if they are records that already exist in your database and your controller only need the reference to them, you'ld be better off passing just the ids (and type if they differ) and having your controller action fetch the records.

If the data in the active records has changed, or isn't yet in the database, I'd have to recommend either using serializing them into a form to submit to the controller action or turning them into json as mentioned above.

The response from your test of #to\_json you showed before "data"=\>"[object Object]" doesn't seem right, as the result should be a string representation of all object's in the array (and all object attributes & values) experiment with it in the console, to get it working, try data.to\_json should work just fine, showing something like:

"[{\"object\_type\": {\"name\": \"Hi\", \"updated\_at\": \"2009-01-16T22:59:51Z\", \"id\": 1, \"summary\_text\": \"awesome!\", \"game\_id\": 1, \"created\_at\": \"2009-01-16T22:59:51Z\"}}]"

where object\_type is the name of the AR model, and all the fields and their values are shown (this example has only one object in the array).
