我有一个发送电子邮件的 Rails 代码。以下是在我的控制器中:
def create
@users = Users.find(:all)
@sub = params[:sub]
@body = params[:body]
@index = 0
@users.each {|i| index++; Notifier.deliver_notification(@users.email_address, @sub, @body, @users.unsubscribe_link);}
flash[:notice] = "Mail was sent to " + @index + " people"
end
我的模型中有以下内容
class Notifier < ActionMailer::Base
def notification(email, sub, content, link)
recipients email
from "my_email@example.com"
subject sub
body :content => recipient, :link => link
end
end
这一切都很好。我的问题是:
例如,如果在向其中一个人发送邮件时出错,即使这样,我的闪信也会说。Mail was sent to X people
我该怎么做才能确保@index
仅在成功发送邮件时才增加?