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

StackOverflow https://stackoverflow.com/questions/4790685

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

有帮助吗?

解决方案

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

    # Callback to overwrite if confirmation is required or not.
    def confirmation_required?
      !confirmed?
    end

把你的条件和工作完成!

亚历克斯

其他提示

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

# Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike
# #skip_confirmation!, record still requires confirmation.
@user.skip_confirmation_notification!

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

def send_confirmation_notification?
  false
end

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

@user.skip_confirmation!

我不知道是否在提交其他答案之后是否添加了此内容,但是此处的代码就在那里 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

我能够在功能上做类似的事情:

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top