2012-10-11 4 views
6

send_confirmation_instructionsをオーバーライドする方法をここに示すように:工夫、私は方法 'send_confirmation_instructions' を上書きしようとしている

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

で:

def send_confirmation_instructions 
    generate_confirmation_token! if self.confirmation_token.nil? 
    ::Devise.mailer.delay.confirmation_instructions(self) 
end 

これはもはやで動作しないようですdeviseの最新バージョン。 deviseのドキュメントはコントローラをオーバーライドする方法を示しますが、モデルは表示しません。開発者モデルを無効にする方法に関する提案はありますか?ありがとう

答えて

7

Deviseをセットアップするときに、どのモデルが動作しているかをユーザーに伝えます。多くの/ほとんどのメソッドは、そのクラスに適用されます。だからこそ、あなたは物事を無効にしたいでしょう。

私が正しく読んでいれば、あなたがしたいことをほぼ正確に記述するlib/devise/models/authenticatable.rbのDeviseコードからのコメントです。

# 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 
+0

私のuser.rbファイルに「send_devise_notification」を追加していただきありがとうございます。私はそれを試して、それが呼び出されなかった... – AnApprentice

+0

はい、あなたのUserモデルで 'send_devise_notification'をオーバーライドします。私はDeviseの最新バージョンで通知を傍受することができました(私はログにsomethigを送って動作を証明しました)。しかし、すべての作業を行うには、コメントを読んでください。メソッドを定義するだけではなく、 'after_commit'フィルタを追加して、遅延ジョブ(または何でも)をビルドする必要があります。 –

+0

ありがとうございますが、これは "send_confirmation_instructions"が存在する場所ではありませんか?そのメソッド "send_confirmation_instructions"を変更する必要があります。 – AnApprentice

0

devise-asyncを使用しない理由は?

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

devise-asyncはユーザーに設定した仮想属性(attr_accessor)。 – AnApprentice

+0

最新のバージョンのdeviseを使用したい場合は、[自分のドキュメント](https://github.com/mhfs/devise-async#devise--40)にあるように、これはオプションではありませんDevise> = 4.0 –

関連する問題