Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: Form getting submitted twice

I am developing a Rails application that has forms generated using formtastic. I am developing & testing locally - that is - on localhost:3000 w/ Ruby-1.9.2, Rails-3.0.1, jQuery and AJAX.

Below is a sample screen output of the problem I am seeing. My forms are getting submitted twice within 1 second of each other. I can't understand why this is happening. I see this issue w/ all requests - including GET.

  1. Started POST "/businesses/6/edit_balance" for 127.0.0.1 at 2011-01-07 02:31:47 +0530 Processing by BusinessesController#edit_balance as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"zcWH08sV8kPbAYy7JQX64Cu2e1i/kEB1AB4x5a08CO8="

  2. Started POST "/businesses/6/edit_balance" for 127.0.0.1 at 2011-01-07 02:31:48 +0530 Processing by BusinessesController#edit_balance as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"zcWH08sV8kPbAYy7JQX64Cu2e1i/kEB1AB4x5a08CO8="

And so I am wondering whether I am making a basic programming error. If yes, then could you please suggest some solutions that I could try.

like image 775
Abhinav Avatar asked Jan 06 '11 21:01

Abhinav


2 Answers

I had this same problem right after deploying to Heroku... I pre-compiled my assets, and all of a sudden I was getting double AJAX submissions. I guess I somehow ended up with duplicate javascript files in public/assets.

To fix the problem I just deleted my entire public/assets directory.

like image 187
Amir Rubin Avatar answered Oct 21 '22 07:10

Amir Rubin


If you're submitting the form with Javascript, try to set the submit button to be disabled when the form is submitted. With jQuery it would be something like this (not tested):

$('form').submit(function(){
  $(this).find(input[type='submit']).attr("disabled", "true");
  ... // submit form via AJAX
  return false;
});
like image 36
polarblau Avatar answered Oct 21 '22 07:10

polarblau