2

I'm building a RESTful web application with ruby + sinatra. The data is stored in a MongoDB database and I use MongoMapper to do this.

One of my models has a nested hash key:

{'Key' => {'ObjectA_id' => ['ObjectB1_id', 'ObjectB2_id', ... , 'ObjectBn_id']}}

As you can see it's a hash whose key is a string and the value is another hash that maps an array of object IDs* (let's call the model Model B) to an id of Model A.

*(these are technically also Strings in fact, cause you cannot store a real BSON ObjectID in a hash)

The problem is that I wouldn't know how to begin to send this data to the server. Normally I use a form like this:

<form action="/create" method="post">
    <input type="text" name="myField"/>
    <input type="submit"/>
</form>

And on the server side I can access whatever the user filled in in myField with this code:

post '/create' do
    myField = params[:myField]
end

The problem is obviously that I can never do this purely with a HTML form, so I wonder: how do I tackle this? I was thinking javascript/jquery, but then I wonder:

  1. How do I simulate a hash in javascript/jquery?
  2. How do I send it with the other form data?
  3. How do I access it on the server and what will it look like?
ian
  • 12,003
  • 9
  • 51
  • 107
MarioDS
  • 12,895
  • 15
  • 65
  • 121

1 Answers1

3

Look at using JSON. It's designed to serialize base JavaScript objects into a string, allowing you to easily send them from one app to another, usually over HTTP. There are JSON parsers for all the major languages, so it is a great tool for data transfer, no matter what an app was written in. Ruby has a good implementation, and a very useful "JSON implementation for Ruby" page showing how to use it:

require 'json'

hash = {'foo' => 'bar'}
serialized_hash = hash.to_json # => "{\"foo\":\"bar\"}"
JSON.parse(serialized_hash) # => { "foo" => "bar" }

JSON also lets you throw an object at it, and get a serialized string, or a serialized string and get an object, so you can shrink all the above down to something like this, showing a full-circle serialization/parse:

JSON[JSON[hash]] # => { "foo" => "bar" }

You can also serialize Ruby objects beyond arrays, hashes, strings and integers:

require 'json/add/core'
JSON[/foo/] # => "{\"json_class\":\"Regexp\",\"o\":0,\"s\":\"foo\"}"

To send the data look at using Open::URI, RestClient, Typhoeus, Curb, or any of the many Ruby HTTP clients.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • This seems the right thing to use, thanks. Could you also show some javascript/jquery please? Just to make it clear how it communicates. – MarioDS Apr 16 '13 at 14:38
  • 1
    No, there are many examples on the internet showing how to do that. Try searching for "send json with jquery". For instance: http://stackoverflow.com/questions/6587221/send-json-data-with-jquery – the Tin Man Apr 16 '13 at 14:40
  • thanks! And how about sending it together with other data in one post, is that possible? – MarioDS Apr 16 '13 at 14:48
  • Can you send a lot of different fields/data from a form via POST? Why wouldn't you be able to do it via AJAX? A POST is a POST. – the Tin Man Apr 16 '13 at 16:59