In rails controllers, how to prevent double submit (when user double-clic submit button or hit enter twice)?

fro_oo picture fro_oo · Jul 1, 2010 · Viewed 12.7k times · Source

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?

Answer

Jason Carpenter picture Jason Carpenter · Jul 17, 2012

You can add the option :disable_with => "Please Wait..." to the submit tag.