【问题标题】:Validation specific email specific domain devise 3 ruby on rails 4验证特定电子邮件特定域设计 3 ruby​​ on rails 4
【发布时间】:2015-05-06 16:34:32
【问题描述】:

我之前发布了一个关于如何使用验证以仅允许使用以@grenoble-em.com 结尾的电子邮件地址的访问者能够在我的网站上注册的问题。我正在使用设计 3 和 Rails 4。我对它相当陌生,希望得到任何答案。这是我的用户模型。

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

提前谢谢大家。你们在帮助人们方面做得很好:)

【问题讨论】:

    标签: validation email ruby-on-rails-4 devise


    【解决方案1】:

    如果您只是想验证电子邮件是否包含该域,而不是其他,您可以添加此验证器:

    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

    【讨论】:

    • 就是这样。非常感谢!
    • 所以当用户注册并且没有以@genny.com 结尾的电子邮件时,会显示一个错误,说该电子邮件无效?
    • 那种。消息内容为:You should have an email from grenoble-em.com
    猜你喜欢
    • 2011-07-28
    • 2015-05-16
    • 2013-12-13
    • 2018-04-30
    • 2022-12-05
    • 2013-09-29
    • 2017-06-22
    • 1970-01-01
    • 2012-08-23
    相关资源
    最近更新 更多