轨道 3 +操作邮件程序 - 无法循环发送电子邮件
用户可以创建一个对象,并且他有追随者,我想在他创建该对象时提醒他们。
控制器:
if @project.save
format.html { redirect_to(@project, :notice => 'Project was successfully created.') }
format.xml { render :xml => @project, :status => :created, :location => @project }
# Send a notification to project owner's followers :
UserMailer.new_project(@project).deliver
else
...
user_mailer.rb:
def new_project(project)
@url = "http://localhost:3000/"
@project = project
# For each of project owner's follower, send an email notification
@followers = project.owner.followers.all
@followers.each do |f|
@u = User.find(f.follower)
mail( :to => @u.email,
:from => '"Beatrix Kiddo" <[email protected]>',
:subject => "#{project.owner.name} created a new project")
end
end
使用拥有 2 个关注者的用户进行测试:User.find(1).followers.count = 2
Follower.follower
是关注者的id
。
仅向第一个关注者发送了 1 封电子邮件,而第二个关注者没有收到任何内容 - 出了什么问题?
[已解决] => .deliver
方法根本不支持多条消息。谢谢灾难恢复
A user can create an object, and he has followers that I want to alert when he creates this object.
controller:
if @project.save
format.html { redirect_to(@project, :notice => 'Project was successfully created.') }
format.xml { render :xml => @project, :status => :created, :location => @project }
# Send a notification to project owner's followers :
UserMailer.new_project(@project).deliver
else
...
user_mailer.rb:
def new_project(project)
@url = "http://localhost:3000/"
@project = project
# For each of project owner's follower, send an email notification
@followers = project.owner.followers.all
@followers.each do |f|
@u = User.find(f.follower)
mail( :to => @u.email,
:from => '"Beatrix Kiddo" <[email protected]>',
:subject => "#{project.owner.name} created a new project")
end
end
Testing with a user that has 2 followers:User.find(1).followers.count = 2
Follower.follower
is the id
of the user who's following.
Only 1 email is sent to the 1st follower, and the 2nd doesn't receive anything - what's wrong?
[SOLVED] => the .deliver
method simply doesn't support multiple messages. Thx DR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ActionMailer 不支持通过一次
deliver
调用发送多条消息。您必须将循环移到
new_project
方法之外:而不是
尝试这样做:
new_project
方法可能如下所示:ActionMailer does not support sending multiple messages with one
deliver
call.You have to move the loop outside of the
new_project
method:Instead of
try this:
The
new_project
method then could look like this:您还可以将 .deliver 调用移至
new_project
方法中。这对我使用 Rails 3 有效:要发送邮件,您可以使用
You can also move the .deliver call into your
new_project
method. This works for me using Rails 3:To send the mails, you would use