Here's an easy way to have all your SimpleForm submit buttons default to
setting data-disable-with so that you don't get errors when users
double click on submit buttons. If you've gotten a few
ActiveRecord::RecordNotUnique errors that were hard to reproduce, then
here's your solution, with our without SimpleForm. Additionally, using
data-disable-with provides the user with nice feedback once a button
If you're using Devise, and you get a ActiveRecord::RecordNotUnique
error when a new user is signing up, where do you look?
At first, I was concerned that my unique index on my users table is not
case insensitive. I started going down the road of converting my normal
unique index on users.email to this index:
CREATEUNIQUEINDEX users_email_ci_idx ON users ((lower(email)));
However, I soon figured out that Devise was already always saving email
in the database in lower case via a before_validation hook.
So then I tried to double click the SAVE button, and, BOOM, I got
the same error.
A little bit of googling quickly revealed some handy rails techniques
disabling a submit button after being clicked, namely the setting of
attribute data-disable-with: "Some Message…" on both links and buttons.
This works nicely to fix the double submit RecordNotUnique error, and it
provides some sweet user feedback upon clicking a button. Here's an
example of a SAVE button.
Immediately after clicking the SAVE button, the button disables and
the text changes.
Opens up the FormBuilder class to add a method
Modifies options hash's :data element, setting a default value for
key disable_with that will not apply if there's already a value
there, thus allowing the default to be overridden by any individual
which makes is so that a call to submit actually calls
submit_with_override and that method can call
submit_without_override, which is the original submit method.
The pattern of naming the methods with_override and
without_override is part of the alias_method_chain call. Pretty
Here's a sample sign-up form that overrides the default "Processing…"
label when the SAVE button is clicked.