Rails 4.0新特性介紹

jieforest發表於2012-06-28
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章