How to stop a user submitting the same data more than once

Good to hear that you’ve solved the problem yourself.
Is now too late to point out that you could probably use
validates_uniqueness_of in your model?

Thanks for the replies.

I’m also not sure if doing the validation in the model would work.
Assuming that the user has a slow connection and hits submit on the form
twice, then the first time the email would be unique, so everything
would be good. However, would it not be the case that the second time
the user presses submit, the record would have been stored, but the
success screen would have not been displayed (hence them feeling the
need to press submit a second time).
In this case the email address would no longer be unique and an error
would be thrown. So, potentially the user could think that their data
had not been submitted, when in fact what they would be seeing was an
error caused by their second submission (their first having been

Nonetheless, thank you very much for the link Aldric. It is definitely
useful to understand what is happening behind the scenes in Active
Record, and it elaborates on the race conditions Fred mentioned. I will
have a play around with validates_uniqueness_of this evening.

I have a similar problem that I’m working on, but I was going to take
a slightly different approach.

My problem is that I’m collecting completely anonymous data at first,
but then I want people to be able to come in later and create an
account, then hopefully be able to find their records. I’m not sure
if this scenario is even realistic, but here is where I am at in my
thinking so far…

The user enters the initial data and their IP address is stored along
with it (could this be my main downfall? I need to do more research on
how these work and if they are truly unique for each user). When they
come back (hopefully on the same computer!) and either try to enter
the data again or go straight to making an account, I want to find a
matching IP and validate that they are a returning user from the Model…
then basically ask them if they’ve been here before, if so yada yada

This is still turning around in my brain and I have yet to do any
research on it…but if anyone see’s any flaws in this method, feel free
to let me know.

Jim - it seems that if you can do your validation in the Model that
would be better practice, but I’m not sure if that suits your needs.

On Aug 27, 2:29 am, Jim B. [email protected]

That’s a cool idea.
I will also have a play around with that this evening.
Thank you very much.

And maybe one more… Again, I know you dislike javascript, but… :slight_smile:

This isn’t exactly generic - I’m pulling it straight out of something
I’m doing right now. Why not show a spinner (and disable the submit
button?) when they press submit and hide the spinner when that’s done ?
It’s all about the feedback, right? :slight_smile:

<%= image_tag(“spinner.gif”,
:align => “absmiddle”,
:border => 0,
:id => “spinner”,
:style =>“display: none;” ) %>

<%= observe_field :date, # The field to observe
:with => :date, # The input to validate
#:on => “onselect”, # What action to check. With JS updates, use
:frequency => 1, # The frequency in seconds to watch for changes
:url => {:action => ‘filter_widget’, :controller => ‘dictated_exams’,
:method => :post },
# The action to call when changes occur
:update => :filter_counts, # Name of the

to update
:before => “‘spinner’)”,
:success => “Element.hide(‘spinner’)”

If you dont want duplicate data then just add the following to the
model. Thats where validation should be.

validates_uniqueness_of :email_address

After talking to some friends of mine, the IP address is NOT a good
idea…looks like I’ll be looking at a cookie solution instead.

The problem is that your users, like most users, are not very bright.
They double-click (or in this case, triple-click) submit
buttons. :disable_with will pretty much solve the issue; anybody with
enough savvy to have JS disabled likely knows that you don’t double-
click submit buttons.

–Matt J.

On Aug 26, 11:09 am, Jim B. [email protected]