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.
function test()
{
for(i=1;i<22;i++){
if (document.getElementById(i+'d').className ==
"selected"){
myArray.push(i);
}
}
new Ajax.Request('/appointments/array/',
{method:'get',postBody:"'data='+myArray.toJSON"});
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.
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:
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:
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.
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
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(){
myArray = new Array();
.......
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:
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?
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.