Rails 3 after_destroy观察者未被调用

时间:2014-03-18 15:30:12

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

这是我的模特......

的应用程序/控制器/ registrations_controller.rb

  def destroy
    @registration = Registration.find(params[:id])
    @registration.cancelled = true
    @registration.save

    respond_to do |format|
      format.html { redirect_to root_url, notice: 'Registration was successfully canceled.' }
      format.json { head :no_content }
      NsoMailer.after_cancellation_email(@registration).deliver
    end
  end

的应用程序/模型/ registration_observer.rb

class RegistrationObserver < ActiveRecord::Observer
#close registration after seats are filled
  def after_create(registration)
    if registration.orientation != nil
      @orientation = registration.orientation
      close_orientation if seats_equal_zero
    end
  end

#opens registration if a registration is removed
  def after_destroy(registration)
    if registration.orientation != nil
      @orientation = registration.orientation
      open_orientation if seats_equal_zero == false
    end
  end

... after_create操作正常,但after_destroy不是。这只是基于名称的正确吗?在观察者中命名动作&after-destroy&#39;链接它相应的控制器动作&#39;销毁&#39;没有?

我还在控制器和观察者操作中添加了puts语句。我正在向控制器操作OK,但不是观察者。

1 个答案:

答案 0 :(得分:1)

它没有将它链接到控制器动作销毁。它将它链接到模型。

为了执行after_destroy,您应该执行

@registration.destroy

您可以进行after_save回调并具有相同的效果

  def after_save(registration)
    return unless registration.cancelled
    if registration.orientation != nil
      @orientation = registration.orientation
      open_orientation if seats_equal_zero == false
    end
  end

您可以查看documentation了解更多信息