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

时间:2011-01-25 06:42:10

标签: ruby email devise confirmation

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

5 个答案:

答案 0 :(得分:21)

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

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

完成你的条件和工作!

亚历

答案 1 :(得分:14)

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

# 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

答案 2 :(得分:11)

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

@user.skip_confirmation!

答案 3 :(得分: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

答案 4 :(得分: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