0

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.

FCD
  • 51
  • 2
  • 9

2 Answers2

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
  • Oh yes, But I took the question regarding validations – Vishal Mar 04 '19 at 12:42
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