2012-05-07 9 views
1

私は私のActionMailers - InvitationMailerのテストを書いていますが、それは"受信者 "メソッドを見つけることができません。ActionMailer:定義されていないメソッド `recipients 'for

私のテストでは、次のようになります

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 

マイInvitationMailerは次のようになります。

class InvitationMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def invitation_email(user) 
    recipients user.email 
    from  "[email protected]" 
    subject  "You have been Invited!" 
    body  :user => user 
    end 

end 

Iただし、次のエラーMSG取得:

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

任意のアイデアをどのようなかもしれない?ここで

+0

どのバージョンのRailsですか? 2.Xを推測する? – x1a4

+0

いいえ、そのレール3.2.2 – Karan

答えて

3

Rails Guide for ActionMailerからの例です:あなたのコードを作る

class UserMailer < ActionMailer::Base 
    default :from => "[email protected]" 

    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: "[email protected]" 

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

次に、:to:subject@userオブジェクトがメーラー 'ビュー'に渡されます。これは他のビューと同じです。

あなたはrecipientsを使用しています。メールを複数のメールアドレスに送信しようとしていたかどうかはわかりませんでした。もしそうなら、actionmailerのドキュメントに従って:

へ 電子メールのリストを設定することにより(例えば、新規サインアップのすべての管理者に通知するための)1つの電子メール 内の1つまたは複数の受信者に電子メールを送信することが可能ですキーを押す。電子メールのリストは、アドレス のアドレスの配列か、アドレスがカンマで区切られた単一の文字列にすることができます。

+0

構文が少し違っているようです。明らかに受信者、fromなどは、あなたの答えからのメソッド呼び出しは使用されていません。代わりに、メールメソッド呼び出しを使用しています。違いはなんですか? (参照http://railscasts.com/episodes/61-sending-email) – Karan

+2

@Newton railscastsは通常素晴らしいリソースですが、もしあなたが古すぎるのであれば、それらの使用について注意する必要があります。あなたが参照しているのは5年前からのものです。一般的に、質問がある場合は常に最新のrailsドキュメントをチェックしてください。 –

+0

ありがとう@Kevin Bedell – Karan

関連する問題