Search code examples
ruby-on-rails-3callbackafter-save

Rails after_save error


Is this the correct way of using after_save callback ?

class CouponsController < ApplicationController
after_save :remove_restrictions
 private
    def remove_restrictions

      logger.debug("in after save")
    end

end

This code throws the error as

undefined method `after_save' for CouponsController:Class

What is the correct way of using after_save ?


Solution

  • app/models/coupon.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
    

    app/controllers/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