1

这是使用 after_save 回调的正确方法吗?

class CouponsController < ApplicationController
after_save :remove_restrictions
 private
    def remove_restrictions

      logger.debug("in after save")
    end

end

此代码将错误抛出为

undefined method `after_save' for CouponsController:Class

使用 after_save 的正确方法是什么?

4

1 回答 1

6

应用程序/模型/优惠券.rb

class Coupon < ActiveRecord::Base
  # after_save goes to your model
  after_save :remove_restrictions

  private

  def remove_restrictions
    logger.debug("in after save")
  end
end

应用程序/控制器/coupon_controller.rb

class CouponController < ApplicationController
  # after_filters goes to your controller
  after_filter :remove_restrictions

  private

  def remove_restrictions
    logger.debug("in after filters")
  end
end
于 2013-11-08T17:31:00.757 回答