这是我的模特......
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
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,但不是观察者。
答案 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了解更多信息