Question

What I need:

Something similar to before_filter in ActionMailer in Rails 3.

Problem:

I am working on Rails 3 and want to have a before_filter in ActionMailer. Checked the actionmailer api and learned about before_action and after_action callbacks. When implemented it gives the error:

NoMethodError: undefined method `before_action' for Notifier:Class

Later learned that there is no before action call for Rails 3 from this post

Isnt there any hook or gem so that we can have something similar like before_filter in Rails 3.

Please help. Many Thanks!!

Was it helpful?

Solution

It can be achieved by including AbstractController::Callbacks. This mimics the change to Rails 4 which apart from comments and tests, just included Callbacks.

class MyMailer < ActionMailer::Base
  include AbstractController::Callbacks

  after_filter :check_email

  def some_mail_action(user)
    @user = user
    ...
  end

  private
   def check_email
     if @user.email.nil?
     message.perform_deliveries = false
    end
    true
   end

end

Reference - How to add a before_filter in UserMailer which checks if it is OK to mail a user?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top