Well, everything's in the title but I'll explain a little more :-)
My rails app contain many forms (Ajaxified or not).
To prevent users to submit twice or more some forms, I use Javascript.
There's my scenario for a Ajaxified form :
- the user submit the form (clic or enter)
- the javascript disable the submit button
- the rails controller do things (like a Soap request or an insert in a DB)
- the rails controller update the page and enable the submit button if necessary (in case of errors)
Now I want to add server side code to keeps things really clean if the user bypass the javascript.
Any suggestions?
I use 4 method for 4 scenarios, please firstly prefer my awnser here: Prevent double submits in a Rails AJAX form
only click limitation for users:
use stopImmediatePropagation and add a click event to the target dom.
limit the submit such as ajax:
use ajax's beforeSend attribut.
only for form:
or: disable:仅仅对表单元素,按钮等起作用,会阻止其上的事件触发
others:
This is the gem:https://github.com/mlanett/redis-lock
Not sure if this is helpful:
ON SERVERSIDE:
On fresh load of the form, set a session['variable']=false //meaning the form isn't submitted yet.
On form submit, check:
I happen to face the same problem as well, and I have solved it with a very simple way(maybe it is not proper or it exists something buggy I didn't notice, please inform me if you found out)
Here is my answer:
The main issue I was facing was that if I double clicked my original button, it would submit request twice and caused something unpredictable, so I tried to block the button with "disabled"attribute right after clicking it. Here is what I wrote.
The biggest problem I was facing is that if I just disable the submit button right after clicking it, the submit request won't submit and the page will just hang there, as nothing ever happens.
I think the cause is that the "disabled" attribute prevents the submit request from submitting.(Although I have no clue what the relationship between these two...)
So, I think the disable event should be executed after submit event.
As far as I understand, form submitting is a Javascript event, and setTimeout is an async method. As Javascript executes based on event loop, the ajax event will be put in the end of the event quene, and will be executed only after all the sync events finish.
In my code, after the first click, the submit button will be disabled after 0 millisecond, which is impossible for human beings to click the second time, problem solved!
Try using Redis locking and surround your block with something like
This is the gem I'm using https://github.com/mlanett/redis-lock
1) It's nice to also show some moving indicator to let user know that something's going on, that their request is being processed. It should eliminate lots of double submits.
2) If user has disabled javascript, how're you gonna submit 'ajaxified' forms? If site becomes not functional without javascript, then it's probably best to just notify user (like Eimantas suggests).
edit One example of such indicator, just to be clear what I mean in 1.
http://www.netzgesta.de/busy/
You can add the option :disable_with => "Please Wait..." to the submit tag.