Rails观察者没有被触发

时间:2014-05-04 17:07:52

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

我正在尝试使用观察者通过电子邮件恢复密码。我的想法是我为UserSession模型将属性设置为true,并且当此属性的值为true并且save的{​​{1}}方法为User时,观察者应该弹出调用

这是观察者:

class UserObserver < ActiveRecord::Observer
    observe :user
    def after_save(user)
        UserNotifier.deliver_forgot_password(user) if user.password_forgotten
    end
end

我添加到configuration/application.rb

config.active_record.observers = :user_observer

我的User模型,它会更改属性的值:

class User < ActiveRecord::Base
  attr_accessor :password_forgotten
  def forgot_password
    self.password_forgotten = true
  end
end

UserSessionController调用forgot_password模型中的User方法。

class UserSessionController < ApplicationController
  def forgot_password
    @user.forgot_password
    @user.save
  end
end

但是,它永远不会从观察者那里到达after_save方法。有什么问题?

0 个答案:

没有答案
相关问题