1

I previously posted a question about how to use validations to only allow visitor using an email address ending with @grenoble-em.com to be able to register on my website. I am using devise 3 and Rails 4. I am fairly new to it and would appreciate any answer. Here is my user model.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
  validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/
  validates :email, uniqueness: true
end

Thank you all in advance. You guys do a great job helping people :)

AdrienNhm
  • 165
  • 1
  • 10

1 Answers1

1

If you simply want to validate that the email contains that domain, and not much else, you can add this validator:

class User < ActiveRecord::Base
  validates_format_of :email, with: /\@grenoble-em\.com/, message: 'You should have an email from grenoble-em.com'
end

You can read more about all the validators here: http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html

ekampp
  • 1,904
  • 18
  • 31
  • So when the user registers and dose not have an email ending in @genny.com an error will show saying that the email is not valid? – James Brown Dec 12 '15 at 08:29
  • Kind of. The message will read: `You should have an email from grenoble-em.com` – ekampp Dec 13 '15 at 16:26