验证具体的电子邮件特定域设计3轨道上的红宝石4 [英] Validation specific email specific domain devise 3 ruby on rails 4
问题描述
我以前发布了一个关于如何使用验证的问题,只允许访问者使用以@ grenoble-em.com结尾的电子邮件地址在我的网站上注册。我使用的是设计3和Rails 4.我是相当新的,会感谢任何答案。这是我的用户模型。
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 :)
推荐答案
如果你只想验证电子邮件包含该域,而不是其他的,你可以添加这个验证器:
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
您可以在这里阅读有关所有验证器的更多信息: http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html
You can read more about all the validators here: http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html
这篇关于验证具体的电子邮件特定域设计3轨道上的红宝石4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!