如何在设计和使用可恢复邮件时将电子邮件设置为可选?

问题描述

电子邮件在设计上是可选的,并且可恢复在以下位置:

class User < ApplicationRecord
  devise :database_authenticatable,:confirmable,:recoverable,:rememberable,:validatable,:trackable,:registerable

  def confirm_if_no_email
    skip_confirmation! if email.blank?
  end

  def email_required?
    false
  end
end
Devise.setup do |config|
  config.reset_password_keys = [:username]
end

但是,当我提交没有电子邮件地址的用户用户名时,我遇到了这个错误: SMTP收件人地址不能为空。

我已经能够通过编辑设备控制器来修复它。有更简单的方法吗?如果没有,请告诉我,以便我可以添加它以设计功能

解决方法

通过覆盖send_reset_password_instructions_notification方法,您可以跳过发送电子邮件的步骤:

# in your User class
def send_reset_password_instructions_notification
  return if email.blank?
  super
end

这也是一个肮脏的技巧,尽管也许你知道。