0

My Rails application includes multiple forms on one page which all relate to the same controller (and in fact the same association). I have one submit button which uses jQuery to submit all of the forms at once through AJAX. The problem is that only some of the updates are actually showing as taking effect in the database. They all use the same logic and the same method and one at a time, the data persists fine. But all sent at once, only about half of the data is actually kept in the database.

The Javascript looks like this:

$(document).on('click', '.submit', function(event){
  $('form').submit();
});

So that submits all the forms via AJAX on one click. (Figure about 10 forms)

The Rails controller looks like this (simplified):

#prepare the JSON data

adapter = DataMapper.repository(:default).adapter
adapter.execute("UPDATE person SET facets=? WHERE id=?", JSON.generate(facets), id)

render :text => 'OK'

I think the problem may involve the fact that multiple processes are trying to access the method at the same time and updating the field with old data. I get the response 'ok' for every request, but only about half of the results take effect.

Any suggestions? Thanks!

EDIT:

Just as an update, in case anyone has this same problem. I fixed the issue in jQuery by setting async to false. This takes the browser a little longer to send and may hold up other javascript events until it's done, but none of the requests will interfere with one another.

Cameron
  • 86
  • 1
  • 2
  • 16
  • 1
    Just suggesting, did you try calling form.submit() in a consecutive order? I mean, send ajax, wait for the 'ok' response, send another ajax and so on until all forms are processed. – JazzJackrabbit Feb 12 '13 at 21:10
  • Huh, I hadn't thought of that. I was looking at a Ruby Mutex, but doing this seems simpler. Thanks for your response. – Cameron Feb 12 '13 at 21:22

1 Answers1

0

This all depends on your browser, the maximum number of parallel HTTP requests is capped, as asked here: Official references for default values of concurrent HTTP/1.1 connections per server? I haven't been able to find an up-to-date reference so if you run into any I'd love to see it!

Hope this helps.

Community
  • 1
  • 1
Novae
  • 1,071
  • 8
  • 17