ActionMailer:未定义的方法`收件人'

时间:2012-05-07 01:04:29

标签: ruby-on-rails actionmailer

我正在为我的一个ActionMailers编写测试 - InvitationMailer,但是,它似乎找不到“收件人”方法。

我的测试如下:

  describe "Invitation" do
    it "should send invitation email to user" do
    user = Factory :user

    email = InvitationMailer.invitation_email(user).deliver
    # Send the email, then test that it got queued
    assert !ActionMailer::Base.deliveries.empty?

    # Test the body of the sent email contains what we expect it to
    assert_equal [user.email], email.to
    assert_equal "You have been Invited!", email.subject

    end

My InvitationMailer看起来像这样:

class InvitationMailer < ActionMailer::Base
  default from: "webmaster@myapp.com"

  def invitation_email(user)
    recipients  user.email
    from        "invitations@myapp.com"
    subject     "You have been Invited!"
    body        :user => user
  end

end

但是,我收到以下错误信息:

 Failure/Error: email = InvitationEmail.invitation_email(user).deliver
 NoMethodError:
   undefined method `recipients' for #<InvitationMailer:0x007fca0b41f7f8>

知道它可能是什么吗?

1 个答案:

答案 0 :(得分:4)

以下是Rails Guide for ActionMailer

的示例
class UserMailer < ActionMailer::Base
  default :from => "notifications@example.com"

  def welcome_email(user)
    @user = user
    @url  = "http://example.com/login"
    mail(:to => user.email,
         :subject => "Welcome to My Awesome Site",
         :template_path => 'notifications',
         :template_name => 'another')
  end
end

让你的代码看起来更像是这样可以更容易解决,所以我首先将其重写为:

class InvitationMailer < ActionMailer::Base
  default from: "webmaster@myapp.com"

  def hassle_email(user)
    @user = user
    mail(:to => user.email,
         :subject => "You have been Invited!")
  end
end

然后,您将:to:subject@user对象传递给邮件程序“视图”,就像任何其他视图一样。

您是否正在使用recipients我不确定您是否尝试将电子邮件发送到多个电子邮件地址。如果是这样,根据ActionMailer文档:

  

可以通过一封电子邮件向一个或多个收件人发送电子邮件   (例如,通过设置列表来通知所有管理员新的注册)   发送电子邮件至:to key。电子邮件列表可以是一系列电子邮件   地址或单个字符串,地址用逗号分隔。