문제

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

도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top