I have deleted forms widely, and in most cases I avoid them. But sometimes your layout or UX requires drop-down forms on the fly, without reloading or refreshing the full page.
So let me do this step by step.
1. Prevention of double message normal form
Even with a normal form, the user can double-click your button or click several times if the user does not receive a clear indication that the click has been registered and the action has begun.
There are many ways (e.g. javascript) to make this visible, but the easiest to use on rails:
= f.button :submit, :disable_with => "Please wait..."
This will disable the button after the first click, clearly indicating that the click has been registered and the action has been started.
2. Remote form processing
For a remote form, this is not so different, but the difference is likely: what happens next?
In a remote form, you have several options:
- In case of error: you are updating the form with errors.
- you leave the form open, allowing users to continue to enter data (I think this is your case?)
- You are redirecting users to some place.
Let me handle these cases. Please understand that these three cases are completely standard in normal form. But not with a successful call.
2.1 In case of error
To set up the remote form correctly, you need to do a little more magic. Not much, but not much.
When using haml, you will have a view called edit.js.haml , which will look something like this:
:plain $('#your-form-id').replaceWith('#{j render(:partial => '_form') }');
What this means: replace full haml with just the form. You will need to structure your views in order to do this job. It is not difficult, but simply necessary.
2.2. Form cleaning
You have two options: * re-render the form completely, as with errors. Just make sure that you visualize the form from a new element, and not just published! * just send the following javascript instead:
$('#your-form-id').reset();
This will close the form and, as a rule, will cause any click to be useless (some client check may block the publication until some fields are filled).
2.3 Redirection
Since you are using a remote form, you cannot just redirect. This should happen on the client side, so this is a little more complicated.
Using haml again, it will be something like
:plain document.location.href = '#{@redirect_uri}';
Conclusion
To prevent double (triple, fourfold, more) messages using remote forms you will have to
- disable the button after the first click (use
:disable_with ) - clear form after successful submission (reset form or rendering with a new element)
Hope this helps.