设计:在特定情况下是否可以发送确认邮件? [英] Devise: Is it possible to NOT send a confirmation email in specific cases ?

查看:219
本文介绍了设计:在特定情况下是否可以发送确认邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我正在使用devit进行登录和注册,它的工作正常,它发送一个真正好的确认电子邮件。但我需要在特定情况下发送确认邮件。我正在传递用户类型的URL,并代表我想要设置邮件。我有两种类型的用户,一个将确认他们的帐户他们的自己重置的用户无法确认他们的帐户只有管理员可以批准他们的帐户。我已经覆盖了创建方法

  def create 
super
如果params [:type] ==' xyz
@ user.skip_confirmation_notification!
end
end

但是在这两种情况下都会发送邮件。请告知哪里错误。

解决方案

所以根据devise可确认模块,您可以通过以下代码跳过发送确认和电子邮件。 / p>

  def confirm_your_type_user_without_confirmation_email 
#在此处查看您的条件并处理以下
self.skip_confirmation!
self.confirm!
#条件可以在这里结束
end

现在可以在创建钩子上调用它。

  class User< ActiveRecord :: Base 
after_create:confirm_your_type_user_without_confirmation_email
....
end

有更多的参考,你可以检查:
Devise可确认模块



该解决方案应与上文所述类似。以及避免控制人处理这些责任的最佳做法,因为它不是你的控制器应该采取的。 :)
我希望我的答案会给你一些方法来解决你的问题!谢谢!


Actually I am using devise for login and registration and its working fine, it send a confirmation email which is really good. But I need to send confirmation email in specific cases. I am passing user type in URL and on behalf of that I want to set mail. I have two type of users, one will be confirm their account their self reset of the users can not confirm their account only admin can approve their accounts. I have override the create method

def create
    super
    if params[:type]=='xyz
        @user.skip_confirmation_notification!
    end
end

but it sends mail in both cases. Please tell where am wrong.

解决方案

So according to devise confirmable module you can skip to send confirmation and email by following code.

 def confirm_your_type_user_without_confirmation_email
     # check your condition here and process the following
     self.skip_confirmation! 
     self.confirm!
     # condition may end here
 end

Now lets call it on create hook.

class User < ActiveRecord::Base
  after_create :confirm_your_type_user_without_confirmation_email
  ....
end

for more reference you may check this: Devise Confirmable module

The solution should be something similar as I mentioned here above. And its best practice to avoid controller to handle these responsibilities, because its not something your controller should take. :) I hope my answer will give you some way to solve your problems! Thanks!

这篇关于设计:在特定情况下是否可以发送确认邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆