5

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?

Bitwise
  • 8,021
  • 22
  • 70
  • 161
  • 1
    One solution would be to use jQuery to disable the button on click, probably the fastest to enable. If you post the html with the button I can write up the solution for you. – Rockwell Rice Jun 05 '18 at 19:59

3 Answers3

4

I think it is similar to this question. And here the oficial documentation

<%= button_tag "Checkout", data: { disable_with: "Please wait..." } %>
3

If you have access to jQuery in your application you can do this

$('BUTTON IDENTIFIER HERE').on('click', function() {
  $(this).prop('disabled', true);
});
Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61
  • What about users who turn javascript off? – Bitwise Jun 05 '18 at 20:05
  • 2
    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. – Rockwell Rice Jun 05 '18 at 20:08
1

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.

jeffdill2
  • 3,968
  • 2
  • 30
  • 47
  • What about a user turning of javascript? – Bitwise Jun 05 '18 at 20:03
  • 1
    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. – jeffdill2 Jun 05 '18 at 20:32