October 8, 2006
Another blindingly obvious way to disable a submit button when pressed
Probably the simplest and least problematic way to disable a submit button when pressed.
-
# put this in your ‘application_helper’ file
-
-
def commit_tag(title, html_options={})
-
submit_tag title, html_options.merge(:id=>’commit’, :onclick=>’$("commit").disabled=true;’)
-
end
Then just use ‘commit_tag’ instead of ’submit_tag’ in your form.
Strictly speaking, you don’t really need to give the button an ‘id’, but I do because it has the bonus side effect of making it easy programmatically disable the button from RJS templates.
One reason this approach is superior (I think) to an AJAX call is that there is less of a chance that an over-enthusiastic user will be able to click the button again before the AJAX response renders.
Filed by Kevin Olbrich at 8:32 am under Ruby on Rails, User Interface
4 Comments
I also use button disabling but what about the already built in :disable_with => “Disabling”
Hit me up on AIM btw Kevin yUncleD
This is certainly a hell of a lot better than an alert box saying “Please, for the love of God, don’t press submit twice!”.
However, it needs to be said that one should not rely on JavaScript to solve the double submit problem. You really need some server-side smarts to detect replays since the user could reload the page and resubmit the form.
True enough. You may really need a belt AND suspenders for this one. There are also some subtle side effects of this method. Notably, it becomes very difficult to determine which button was pressed if you have more than one.
Even more obvious would be “this.disabled=true;” ;-)
But this “disabled” solution may prevent the form being submitted in Safari. (Maybe my Safari has a bad day today?)
:disable_with => “Processing…” works like a breeze!