I have a user created array which is displayed by a function in a table (this is all done with jquery on the client side). I want to save this to the database so it can be linked to and displayed again. I have my rails model and form setup to accept a string (presumably I must convert the array to a string) but I'm entirely unsure how to code this properly. I've followed this tutorial but I'm not sure where it's going wrong. I don't need an exact answer, just looking to be pointed in the right direction..
1 Answer
JavaScript
var arrayAsString = JSON.stringify(myArray);
sendToDatabaseUsingAJAX( arrayAsString );
// ... later ...
var arrayFromString = JSON.parse( arrayAsString );
Ruby
require 'json'
array_as_string = my_array.to_json
send_to_database( array_as_string )
# ... later ...
array_from_string = JSON.parse( array_as_string_gotten_from_db )