Rails 4.0新特性介绍

My previous deep dive into the Rails 4.0 Queueing system was motivated by a patch to Rails I was working on while at RailsCamp New England this past weekend. I'm happy to say that Rails 4.0 now has an optional asynchronous ActionMailer.

The API for pushing your emails to the background is very simple. If you want to make this change application wide simply set it in your application.rb (or in any of the environment files)

CODE:

config.action_mailer.async = trueOr if you want to only make specific mailers asynchrounous

CODE:

class WelcomeMailer < ActionMailer::Base
  self.async = true
endThat's it! Any messages that are being delivered will be sent as a background job. In fact, the rendering is happening on the background as well.
You will need to take care that the arguments you are passing your mailers can be properly marshalled. Instead of:

CODE:

WelcomeMailer.welcome(@user).deliverYou should do:

CODE:

WelcomeMailer.welcome(@user.id).deliverThen in your mailer:

CODE:

class WelcomeMailer < ActionMailer::Base
  def welcome(id)
    @user = User.find(id)
    ...
  end
end

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/301743/viewspace-734050/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/301743/viewspace-734050/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值