I have a pretty generic controller and form. But for some reason I can double click on my form submit button and it will create two identical records. I would show code but there is nothing out of the ordinary that I'm doing. Is this a well known issue with rails? How can I make sure that the creation process for a record is synchronous and doesn't let users create multiple records on a double click?
3 Answers
If you have access to jQuery in your application you can do this
$('BUTTON IDENTIFIER HERE').on('click', function() {
$(this).prop('disabled', true);
});
2 Comments
Bitwise
What about users who turn javascript off?
Rockwell Rice
Well by double clicking fast you are sending 2 post requests. It has nothing to do with rails, it is just you get 2 fired off before anything happens. Not sure what else you could do to stop it just with rails, you need the button to be enabled and then after a post to be disabled, if they double click really fast it will be hard. The only rails way I can think of would be to check if the record is already created, but that depends on what is being saved as to whether or not it would even be an option.
It's not a Rails issue. It's an issue that's applicable in any frontend.
You'll need to disable the submit button when it's clicked.
2 Comments
Bitwise
What about a user turning of javascript?
jeffdill2
Yup, one of the many issues that can be caused by disabling JS. But since JS and the web are pretty much synonymous in 2018 – meaning it shouldn't even be expected for a site to operate at all sans JS – unlike 15 years ago when JS was still used as an enhancement, I just chalk it up to something that I choose not to support. Obviously, based on your customer base, you might not have the luxury of ignoring that fraction of users.