I have a 2d array in javascript which I have to send to sinatra and store using datamapper, I've been trying to put them in forms, but its a bit difficult to do it with forms, can I send an array to sinatra and process it?
1 Answer
You can send it to Sinatra encoding the content as a JSON string and passing it using the body part of a POST request.
Here's how the string should look like.
require 'json'
JSON.dump([[1,2,3], [4,5,6], [7,8,9]])
# => "[[1,2,3],[4,5,6],[7,8,9]]"
Send the content to Sinatra (using curl or any other HTTP library)
$ curl -d "[[1,2,3],[4,5,6],[7,8,9]]" http://localhost:3000/loader
Then instruct Sinatra to handle the path
post '/loader' do
json = JSON.load(request.body.read)
# ... do something with json
end
Note. I haven't tried the code actually, so you might need small changes to make it working. This is just a prototype.
1 Comment
pahnin
thanks, putting together your answer and @raina77ow 's answer JS/JQUERY:
$.post("/save/sources",JSON.stringify(2darray),function(data){console.log(data)}); RUBY: post '/save/sources' do json=JSON.load(request.body.read) json.each{|j| }