设计:在特定情况下是否可以不发送确认电子邮件?(即使确认活动时)

Ale*_*lex 14 ruby email confirmation devise

这是我的情况,我使用设计允许用户在我的网站上创建帐户并管理他们的身份验证.在注册过程中,我允许客户更改一些选项,从而导致创建一个实际上不同的帐户,但仍然基于相同的核心用户资源.我想选择不发送某些帐户类型的确认电子邮件.我不在乎帐户是否未得到确认且用户无法登录,这没关系,没有pb.我该怎么做呢?谢谢,亚历克斯

Ale*_*lex 22

实际上,一旦我深入挖掘,这很容易.只需覆盖用户模型中的一个方法(或您正在使用的任何方法):

    # Callback to overwrite if confirmation is required or not.
    def confirmation_required?
      !confirmed?
    end
Run Code Online (Sandbox Code Playgroud)

把你的条件和工作完成!

亚历克斯


kma*_*ana 14

如果您只想跳过发送电子邮件但未进行确认,请使用:

# Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike
# #skip_confirmation!, record still requires confirmation.
@user.skip_confirmation_notification!
Run Code Online (Sandbox Code Playgroud)

如果您不想在模型中使用回调调用此方法,请覆盖此方法:

def send_confirmation_notification?
  false
end
Run Code Online (Sandbox Code Playgroud)


Jos*_*ris 11

您也可以在创建新用户之前在控制器中添加以下代码行:

@user.skip_confirmation!
Run Code Online (Sandbox Code Playgroud)


Tur*_*adg 8

我不知道Devise是否在提交了其他答案之后添加了这个,但是代码就在那里confirmable.rb:

  # If you don't want confirmation to be sent on create, neither a code
  # to be generated, call skip_confirmation!
  def skip_confirmation!
    self.confirmed_at = Time.now
  end
Run Code Online (Sandbox Code Playgroud)

  • 请注意,您应该在保存用户之前调用它,否则将发送“确认说明”电子邮件。 (2认同)
  • 还有一个类似的`skip_reconfirmation!`方法 (2认同)

bon*_*fer 5

我能够做类似的功能:

registrations_controller.rb

def build_resource(*args)
    super
    if session[:omniauth] # TODO -- what about the case where they have a session, but are not logged in?
      @user.apply_omniauth(session[:omniauth])
      @user.mark_as_confirmed # we don't need to confirm the account if they are using external authentication
      # @user.valid?
    end
  end

然后在我的用户模型中:

user.rb

  def mark_as_confirmed
    self.confirmation_token = nil
    self.confirmed_at = Time.now
  end