for my rails3, devise, users model (name, email, etc…) I want to prevent bad domains from registering on the site.
The idea being I have a list of blacklisted domains (badplace.com, hotmail.com) … and when a new user record goes to be saved, I check the email, if it has a domain with a bad domain, I add an error.
So what’s the right way to implement this smartly in Rails…
Here’s what I’ve been playing with:
In the User’s Model
protected
validates_each :email, :on => :create do |record, attr, value|
domain = email.split("@").last
record.errors.add attr, "That's a BAD EMAIL." unless value && !value.contains(domain)
end
What do you think?
You can do this more easily with a
validates_format_ofand a regular expression:EDIT:
For many addresses, you can do something like this: