过滤之前的邮件Rails 3 [英] Before filter in action mailer Rails 3
问题描述
我需要:
What I need:
与Rails 3中的ActionMailer中的before_filter类似。
Something similar to before_filter in ActionMailer in Rails 3.
问题:
Problem:
我正在Rails 3在ActionMailer中有一个before_filter。选中了 actionmailer api 并了解了before_action和after_action回调。实现时会产生错误:
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
后来发现没有之前的操作调用Rails 3从这 post
Later learned that there is no before action call for Rails 3 from this post
没有任何钩子或gem,所以我们可以像before_filter in Rails 3。
Isnt there any hook or gem so that we can have something similar like before_filter in Rails 3.
请帮忙。非常感谢!
推荐答案
它可以通过包括AbstractController :: Callbacks来实现。这模仿了Rails 4的改变,除了评论和测试,只是包括回调。
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
参考 - 如何在UserMailer中添加before_filter,检查是否可以邮寄用户?
这篇关于过滤之前的邮件Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!