# Problem with form update and multiple submit

**URL:** <https://www.ruby-forum.com/t/problem-with-form-update-and-multiple-submit/119081>\
**Category:** Rails\
**Created:** [November 2, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/problem-with-form-update-and-multiple-submit/119081 "2007-11-02T22:44:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 2, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/problem-with-form-update-and-multiple-submit/119081/1 "2007-11-02T22:44:06Z")

</div>

Here is what I am doing. I do a form\_remote\_tag in order to update my  
data.

I want to be able to prevent the user from going back to the form  
with the browser back button, so by having it ajax  
after the submit finishes I do a partial render to remove the form  
from the page and the browsers cached version of that page no longer  
has a form which is what I want.

The problem is that before the ajax call finishes, they could do  
multiple hits of the submit button which will send the data to the  
browser. I suspect there may be a better way to do this, not sure if  
I’d have to write custom javascript however ?

Basically what I do is that I set a session variable  
update\_in\_progress=true, then in the base controller if it’s not an  
ajax call I set that to false. That seems to work fine as if I am in  
my update(), if that is set it means the submit was hit twice and I  
just ignore it. The problem is that I still have to handle so sort of  
rendering that happens on the unwanted submits.

If I do render :nothing =\> true, it blows away anything that the  
previous render partial had done from  
the first submit. So if it had said “record updated successfully” that  
then disappears.

If I do

render :update do |page|

# do nothing

end

that seems to put some sort of javascript garbage onto my page, which  
I have had problems with that in other places as well.

If I just call exit() to get out of the controller, that causes a  
stack dump

Maybe there’s an easy answer here, but I don’t know what it is as of  
yet …

thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 3, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/problem-with-form-update-and-multiple-submit/119081/2 "2007-11-03T16:23:16Z")

</div>

If you just want to prevent multiple clicks on submit button or link  
then simply add an onclick handler to hide that button or link and  
show a progress bar that will prevent your application from receiving  
multiple submits.

For example, go to [www.sphred.com](http://www.sphred.com) and click on Sign In or Sign Up link  
then try signing in or up. The same is followed here.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 3, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/problem-with-form-update-and-multiple-submit/119081/3 "2007-11-03T17:18:17Z")

</div>

> The problem is that before the ajax call finishes, they could do  
> multiple hits of the submit button which will send the data to the  
> browser. I suspect there may be a better way to do this, not sure if  
> I’d have to write custom javascript however ?

You can use the :condition and :before options. :conditions allows you  
to specify a condition, if it evaluates to false, the request is not  
made. :before specifies some javascript that is run before the request  
is made. You could check a flag in :condition and set it in :before  
(and maybe clear it in :complete)

> then disappears.  
> If I just call exit() to get out of the controller, that causes a  
> stack dump

I’m guessing you’re using :update so that the ajax call updates a  
certain div. If that’s the case then there is no solution. You have  
to be using not :update in order to have any sort of conditional stuff  
like this.

You still can’t guard against the user having the form visible in 2  
separate browser windows and submitting from each one.  
fred
