轨道 3 +操作邮件程序 - 无法循环发送电子邮件

发布于 2024-10-21 12:51:42 字数 1402 浏览 5 评论 0原文

用户可以创建一个对象,并且他有追随者,我想在他创建该对象时提醒他们。

控制器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海之角 2024-10-28 12:51:42

ActionMailer 不支持通过一次 deliver 调用发送多条消息。
您必须将循环移到 new_project 方法之外:

而不是

UserMailer.new_project(@project).deliver

尝试这样做:

@followers = @project.owner.followers.all
@followers.each do |f|
    UserMailer.new_project(@project, f).deliver
end

new_project 方法可能如下所示:

def new_project(project, follower)
    @u = User.find(f.follower)
    mail(:to => @u.email,
         :from => '"Beatrix Kiddo" <[email protected]>',
         :subject => "#{project.owner.name} created a new project")
end

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

UserMailer.new_project(@project).deliver

try this:

@followers = @project.owner.followers.all
@followers.each do |f|
    UserMailer.new_project(@project, f).deliver
end

The new_project method then could look like this:

def new_project(project, follower)
    @u = User.find(f.follower)
    mail(:to => @u.email,
         :from => '"Beatrix Kiddo" <[email protected]>',
         :subject => "#{project.owner.name} created a new project")
end
初懵 2024-10-28 12:51:42

您还可以将 .deliver 调用移至 new_project 方法中。这对我使用 Rails 3 有效:

def new_project(project)
  # ...
  @followers.each do |f|
    # ...
    mail(:to => @u.email,
         :from => '"Beatrix Kiddo" <[email protected]>',
         :subject => "#{project.owner.name} created a new project").deliver
  end
end

要发送邮件,您可以使用

if @project.save
   # ...
   UserMailer.new_project(@project)
else
   # ...
end

You can also move the .deliver call into your new_project method. This works for me using Rails 3:

def new_project(project)
  # ...
  @followers.each do |f|
    # ...
    mail(:to => @u.email,
         :from => '"Beatrix Kiddo" <[email protected]>',
         :subject => "#{project.owner.name} created a new project").deliver
  end
end

To send the mails, you would use

if @project.save
   # ...
   UserMailer.new_project(@project)
else
   # ...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文