首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Devise,如何覆盖send_confirmation_instructions

Devise,如何覆盖send_confirmation_instructions
EN

Stack Overflow用户
提问于 2012-10-11 10:44:54
回答 2查看 8.7K关注 0票数 7

我尝试重写方法'send_confirmation_instructions‘,如下所示:

http://trackingrails.com/posts/devise-send-confirmation-mail-manually-or-delay-them

通过以下方式:

代码语言:javascript
复制
def send_confirmation_instructions
    generate_confirmation_token! if self.confirmation_token.nil?
    ::Devise.mailer.delay.confirmation_instructions(self)
end

这似乎不再适用于最新版本的devise。devise文档展示了如何覆盖控制器,而不是模型。关于如何覆盖设计模型有什么建议吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-11 10:59:22

当你设置Devise时,你告诉它它正在使用哪个模型(例如User);然后它的许多/大多数方法都应用于那个类。这就是你想要重写东西的地方。

如果我没看错的话,这是来自lib/devise/models/authenticatable.rb的设计代码的注释,它几乎准确地描述了您想要做的事情。

代码语言:javascript
复制
  # This is an internal method called every time Devise needs
  # to send a notification/mail. This can be overriden if you
  # need to customize the e-mail delivery logic. For instance,
  # if you are using a queue to deliver e-mails (delayed job,
  # sidekiq, resque, etc), you must add the delivery to the queue
  # just after the transaction was committed. To achieve this,
  # you can override send_devise_notification to store the
  # deliveries until the after_commit callback is triggered:
  #
  #     class User
  #       devise :database_authenticatable, :confirmable
  #
  #       after_commit :send_pending_notifications
  #
  #       protected
  #
  #       def send_devise_notification(notification)
  #         pending_notifications << notification
  #       end
  #
  #       def send_pending_notifications
  #         pending_notifications.each do |n|
  #           devise_mailer.send(n, self).deliver
  #         end
  #       end
  #
  #       def pending_notifications
  #         @pending_notifications ||= []
  #       end
  #     end
  #
  def send_devise_notification(notification)
    devise_mailer.send(notification, self).deliver
  end
票数 9
EN

Stack Overflow用户

发布于 2012-10-11 11:06:55

为什么不使用devise-async

代码语言:javascript
复制
Usage

Devise >= 2.1.1

Include Devise::Async::Model to your Devise model

class User < ActiveRecord::Base
  devise :database_authenticatable, :confirmable # etc ...

  include Devise::Async::Model # should be below call to `devise`
end

Devise < 2.1.1

Set Devise::Async::Proxy as Devise's mailer in config/initializers/devise.rb:

# Configure the class responsible to send e-mails.
config.mailer = "Devise::Async::Proxy"

All

Set your queuing backend by creating config/initializers/devise_async.rb:

# Supported options: :resque, :sidekiq, :delayed_job
Devise::Async.backend = :resque
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12831419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档