Kevin Whitaker
Kevin Whitaker

Reputation: 13435

Rails: Prevent duplicate entry from simultaneous submission

We're having a problem with our app allowing people to sign up multiple times with the same account information (email, specifically).

Our user model validates the uniqueness of the email parameter, and we are also using some javascript to make sure that once the "sign up" button is clicked, it becomes unusable unless the sign up fails (theoretically ensuring only a single click).

It appears that the problem stems from users double-clicking the signup button before the javascript on the page finishes loading.

Is there a way from the Rails side that we can prevent this? Maybe something that creates a request stack, and then iterates through them? I ask because we can't be the only site that has this issue.

Thanks

Upvotes: 1

Views: 877

Answers (4)

Rhywden
Rhywden

Reputation: 750

Dumb question: Why don't you set the field in the database itself to unique?

If that is not possible, do what Steve Bourne suggested and use something like this:

var clicked = false;
$('#submit_button').click( function() {
  $(this).preventDefault();
  if(!clicked) {
    clicked = true;
    $('#submit_button').attr('disabled','disabled');
    $('#form').submit();
}

Now, I didn't test that so setting clicked = true may be overkill ;)

Upvotes: 2

Steve Bourne
Steve Bourne

Reputation: 951

If the problem is that users submit the form before your javascript is finished loading, why not make that impossible? (i.e. the submit button action doesn't submit the form, but your javascript submits on the click event?)

There are a few ways to then prevent dupes in JS; sounds like you've got that covered already.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160261

Not entirely convinced that's what's happening, but another option would be to only enable the submit button in jQuery's ready function, and start off with it hidden or disabled. Are you running a large amount of JS outside of the ready function?

Upvotes: 0

Jason
Jason

Reputation: 1

Set the text field to Nothing right after the insert.

Upvotes: 0

Related Questions