# Convert javascript array to ruby array

**URL:** <https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141>\
**Category:** rubyonrails-talk\
**Created:** [September 8, 2008, 9:52pm UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141 "2008-09-08T21:52:34Z")\
**Posts on this page:** 12\
**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:** [September 8, 2008, 9:52pm UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/1 "2008-09-08T21:52:34Z")

</div>

Hey can anyone help me convert a javascript array into a ruby array. Ive been struggling with this since friday to no avail. This is the function with the ajax.request call that is supposed to convert the array using JSON, but I keep getting a 422(Unprocessable Entity) error. Im not sure what is wrong.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function test() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(i=1;i\<22;i++){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (document.getElementById(i+'d').className == "selected"){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myArray.push(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

new Ajax.Request('/appointments/array/', {method:'get',postBody:"'data='+myArray.toJSON"});

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

Thanks for any input!

---

<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:** [September 8, 2008, 9:54pm UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/2 "2008-09-08T21:54:06Z")

</div>

Sorry, that ajax.request method should be a put, so:

new Ajax.Request('/appointments/array/', {method:'post',postBody:"'data='+myArray.toJSON"});

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 9, 2008, 1:30am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/3 "2008-09-09T01:30:52Z")

</div>

> Sorry, that ajax.request method should be a put, so:
> 
> new Ajax.Request('/appointments/array/', {method:'post',postBody:"'data='+myArray.toJSON"});

If you're just posting json then you shouldn't that data= (and you  
look like you're writing a link\_to\_remote :with option - right now  
you're posting the string "'data='+myArray.toJSON")

something like

postBody: Object.toJSON(myArray)

is more like it. You might also need to set the content-type header  
appropriately.

Fred

---

<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:** [September 9, 2008, 1:42am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/4 "2008-09-09T01:42:01Z")

</div>

Yea, I am trying to just use the link\_to\_remote helper now, but aside from converting the array and using JSON, I can't even get the link\_to\_remote to route now. I am using:

\<%= link\_to\_remote 'Click me', :url =\> {:action =\> "update"}, :with =\> 'test' %\>

and the only action that works successfully is the new action, so

\<%= link\_to\_remote 'Click me', :url =\> {:action =\> "new"}, :with =\> 'test' %\>

routes successfully, but this is not what i want. I get this error in firebug:

POST [http://localhost:3000/appointments/update500](http://localhost:3000/appointments/update500) 241ms.

Do you have any idea why this is happening?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 9, 2008, 1:45am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/5 "2008-09-09T01:45:33Z")

</div>

> Yea, I am trying to just use the link\_to\_remote helper now, but aside from converting the array and using JSON, I can't even get the link\_to\_remote to route now. I am using:
> 
> \<%= link\_to\_remote 'Click me', :url =\> {:action =\> "update"}, :with =\> 'test' %\>
> 
> and the only action that works successfully is the new action, so
> 
> \<%= link\_to\_remote 'Click me', :url =\> {:action =\> "new"}, :with =\> 'test' %\>
> 
> routes successfully, but this is not what i want. I get this error in firebug:
> 
> POST [http://localhost:3000/appointments/update500](http://localhost:3000/appointments/update500) 241ms.

I assume that's telling you that the server returned a 500 error,  
which means check your logs.

Fred

---

<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:** [September 9, 2008, 5:22am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/6 "2008-09-09T05:22:01Z")

</div>

okay, thanks for the heads up. The error says, TypeError (exception object expected). Not sure what this means, do you have any idea?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 9, 2008, 7:55am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/7 "2008-09-09T07:55:35Z")

</div>

> okay, thanks for the heads up. The error says, TypeError (exception object expected). Not sure what this means, do you have any idea?

Usually it means you tried to raise something that isn't an exception, but without a backtrace or seeing the line that cause this error it's hard to say.

Fred

---

<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:** [September 9, 2008, 8:03am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/8 "2008-09-09T08:03:27Z")

</div>

This is the line that caused the error: \<%= link\_to\_remote 'Click me', :url =\> {:controller =\> "appointments", :action =\> "array"}, :with =\> "test"%\>. These are the lines that follow the error: TypeError (exception object expected): &nbsp;&nbsp;&nbsp;&nbsp;/app/controllers/appointments\_controller.rb:72:in `raise' &nbsp;&nbsp;&nbsp;&nbsp;/app/controllers/appointments_controller.rb:72:in `array' &nbsp;&nbsp;&nbsp;&nbsp;C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/ actionpack-2.0.2/lib/action\_controller/base.rb:1158:in `send' &nbsp;&nbsp;&nbsp;&nbsp;C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/ actionpack-2.0.2/lib/action_controller/base.rb:1158:in `perform\_action\_without\_filters' &nbsp;&nbsp;&nbsp;&nbsp;C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/ actionpack-2.0.2/lib/action\_controller/filters.rb:697:in `call_filters' &nbsp;&nbsp;&nbsp;&nbsp;C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/ actionpack-2.0.2/lib/action_controller/filters.rb:689:in `perform\_action\_without\_benchmark' &nbsp;&nbsp;&nbsp;&nbsp;C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/ actionpack-2.0.2/lib/action\_controller/benchmarking.rb:68:in `perform_action_without_rescue' &nbsp;&nbsp;&nbsp;&nbsp;C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'

Not sure if this is helpful at all. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 9, 2008, 8:07am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/9 "2008-09-09T08:07:46Z")

</div>

> This is the line that caused the error: \<%= link\_to\_remote 'Click me', :url =\> {:controller =\> "appointments", :action =\> "array"}, :with =\> "test"%\>. These are the lines that follow the

No, that line didn't cause the error. That piece of code worked just fine and generated a link you clicked on, and processing that request caused the error

> error: TypeError (exception object expected): /app/controllers/appointments\_controller.rb:72:in `raise' /app/controllers/appointments_controller.rb:72:in `array'

This is the line causing the error (and it looks like you're calling raise yourself)

Fred

---

<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:** [September 9, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/10 "2008-09-09T09:40:57Z")

</div>

Yea, you were right, its been a long day....but i have finally figured it out. Thanks for your help. For anyone who is trying to convert a javascript array to a ruby array and pass the array to the controller you can follow this advice: create a javascript function that returns the array with the toJSON() function, so like this: function example(){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myArray = new Array(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;....... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return myArray.toJSON() }

you can then send this converted array to an action using the link\_to\_remote ajax helper by calling your javascript function within the :with parameter like so:

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

Your array will then be accessible in params[:data] in you action (array in this case)

---

<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:** [September 9, 2008, 9:38pm UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/11 "2008-09-09T21:38:04Z")

</div>

So it turns out the call myArray.toJSON() only converts the javascript array myArray to a string. I have implemented a work around parse to convert the string into an array, but i was wondering if anyone knows of a JSON method to convert directly to a ruby array. Maybe this would be faster?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 10, 2008, 5:39am UTC](https://discuss.rubyonrails.org/t/convert-javascript-array-to-ruby-array/34141/12 "2008-09-10T05:39:34Z")

</div>

> So it turns out the call myArray.toJSON() only converts the javascript array myArray to a string. I have implemented a work around parse to convert the string into an array, but i was wondering if anyone knows of a JSON method to convert directly to a ruby array. Maybe this would be faster?

What would that even mean? Everything the browser sends to the server is serialized in some format - there's no escaping that.

Fred
