Before filter in action mailer Rails 3

2019-06-17 05:16发布

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!!

1条回答
放荡不羁爱自由
2楼-- · 2019-06-17 05:44

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?

查看更多
登录 后发表回答