0

In my rails app, I have a long Inventory form that I want to autosave. I used the code from this post. This creates the entry, but continues to create more entries since I have not changed the action to update. What do I add to the function so that Rails recognizes the next autosave as an update and not a create? Here is the javascript from the linked post:

setInterval(function(){
  var form = $('#my-form-id');
  var method = form.attr('method').toLowerCase();      // "get" or "post"
  var action = form.attr('action');                    // url to submit to
  $[method](action, form.serialize(), function(data){
  // Do something with the server response data      
  // Or at least let the user know it saved
  });
},10000);      
Community
  • 1
  • 1
NicSlim
  • 339
  • 3
  • 12

1 Answers1

1

This is what I would do:

  1. Have the create return a json that contains the ID of the newest entry
  2. If the autosave detects an ID on the json return, change form.attr('action') to update

Hope this helps

rickypai
  • 4,016
  • 6
  • 26
  • 30
  • Thanks. Getting a JSON response is definitely the way to go. The tricky part that I'm still figuring out is how to use the ID to get the form to submit to the Update action after the first autosave. – NicSlim Aug 07 '12 at 00:18
  • the path should be /model_name/id for the action. or just look into the update page of what you've built. – rickypai Aug 07 '12 at 02:26