Using the Simple Form gem, how can I only allow a user to submit the form only if their email has not yet been used to submit a form.
Asked
Active
Viewed 125 times
0
-
1Add some code to your question. What have you tried? – mechnicov Mar 04 '19 at 10:45
-
I found this code online ´validates :name, uniqueness: { case_sensitive: false }´ but I don't know where to put it. I tried to put it in the post controller and on the actual form. But it didn't work. – FCD Mar 04 '19 at 10:47
-
Check https://stackoverflow.com/questions/12328873/how-to-check-user-email-uniqueness-and-pass-result-to-jquery – Vasilisa Mar 04 '19 at 10:48
-
It must be possible without jQuery? – FCD Mar 04 '19 at 10:54
-
@FCD In your User model. – Umar Khan Mar 04 '19 at 11:01
2 Answers
1
In addition to Venom's answer, I recommend using unique index.
Because there may be a situation when there will be requests with the same email at the same time. And they will be validated. In this case, you will have duplicates.
For this purpose make constraints. It will be something like
$ rails g migration AddIndexToObjects
class AddIndexToObjects < ActiveRecord::Migration
def change
add_index :objects, :email, unique: true
end
end

mechnicov
- 12,025
- 4
- 33
- 56
-
Yes, this constraint should be used for email, but he was confused in where and how to use validates :email, uniqueness: true. – Vishal Mar 04 '19 at 12:33
-
Have you read the question? 'get posted if the email has not been used in a post before. How can I do this?' – mechnicov Mar 04 '19 at 12:36
-
0
Use it in your model which has an email field, and not in your controller, add the following code
validates :email, uniqueness: true
what does this code do?
This will prevent users to use email id which are already present in your database. don't forget to check right if I'm correct.
Thanks.

Vishal
- 818
- 8
- 20