2012-02-10 25 views
0

Rails 2.3.14アプリケーションをRails 3.0に移行しています。その中で、メーラーは添付ファイル付きのメッセージを送信します。以下のコードを使用して、これは2.3.xで問題なく動作しました。 Railsの3.0 ActionMailer instructionsを通じて読むRails 3 ActionMailerが添付ファイルを受け取ることができません

def notification(material, recipient, path_to_file) 
    enctype = "base64" 

    @recipients = recipient.email 
    @from  = material.person.email 
    @reply_to = material.person.email 
    @subject  = "New or updated materials: " + material.name 
    @sent_on  = Time.now 
    @content_type = "multipart/mixed" 
    @headers['sender'] = material.person.email 

    part :content_type => "text/plain", 
      :body => render_message('notification', 
      :material => material, 
      :url => material.full_url_to_material) 

    attachment :content_type => "application" + "/" + material.file_type, 
       :body => File.read(path_to_file), 
       :filename => File.basename(material.file), 
       :transfer_encoding => enctype, 
       :charset => "utf-8" if !!material.send_as_attachment 

end 

は、私は次のようにメソッドを変更した:材料が作成されたときに

def notification(material, recipient, path_to_file) 
    @material = material 
    @url = material.full_url_to_material 
    attachments[material.file_file_name] = File.open(path_to_file, 'rb'){|f| f.read} if material.send_as_attachment? 
    headers['sender'] = material.person.email 
    mail(:to => recipient.email, 
     :subject => "New or updated materials: " + material.name, 
     :reply_to => material.person.email, 
     :from => material.person.email) 
    end 

MaterialMailer#通知が呼び出されます。私はこれをテストするために次の仕様を持っています:

it "will include the materials as an attachement with the the send_as_attachment field is set to 1" do 
    it = Material.create(@materials_hash.merge(:send_notification => "1", :send_as_attachment => "1")) 
    email = ActionMailer::Base.deliveries[0] 
    email.body.should =~ Regexp.new("Name of the posted material: " + it.name) 
    email.has_attachments?.should be_true 
    end 

これは2.3でうまくいきました。私は1つにsend_as_attachmentフラグを設定している場合今、私はemail.body.shouldラインを参照し、次のエラーが発生します。

1) Material will include the materials as an attachement with the the send_as_attachment field is set to 1 
    Failure/Error: email.body.should =~ Regexp.new("Name of the posted material: " + it.name) 
     expected: /Name of the posted material: My material/ 
      got: (using =~) 
     Diff: 
     @@ -1,2 +1 @@ 
     -/Name of the posted material: My material/ 

私は仕様を変更し、0にsend_as_attachmentを設定した場合、私は次のエラーを取得しますhas_attachmentsを参照していますか?ライン:

1)材料はsend_as_attachmentフィールドが1 失敗/エラーに設定されているとの添付ファイルのような物質が含まれます:?email.has_attachmentsだから含む

本当であるにはfalseを期待 をbe_true .should添付ファイルが何とかメールを破っています。

私は材料を取り付けるための他の方法を試してみました:私は、既知のファイルへのファイルパスをハードコーディングしようとしている

attachments[material.file_file_name] = {:mime_type => "application" + "/" + material.file_content_type, 
       :content => File.read(material.file.path), 
       :charset => "utf-8"} 

。しかし、運がない。

その他はどこですか?

+1

アイロニーのアイロニーは、この作品を仕上げた後、私はそれを理解しました。私は身体の電子メールの最初の部分を見る必要があります。 – TDH

+0

mail.parts [0] .body.should =〜Regexp.new( "投稿されたマテリアルの名前:" + it.name) – TDH

+1

これを実行した人の回答を投稿して、それ :} –

答えて

0

パークリストファーの提案、ここで私はそれが仕事を得るために使用メーラーやスペックコードです:スペックで

def notification(material, recipient, path_to_file) 
    @material = material 
    @url = material.full_url_to_material 
    attachments[material.file_file_name] = File.open(material.file.path, 'rb'){|f| f.read} if material.send_as_attachment? 
    headers['sender'] = material.person.email 
    mail(:to => recipient.email, 
     :subject => message_subject("New or updated materials: " + material.name), 
     :reply_to => material.person.email, 
     :from => material.person.email) 
    end 

    it "will include the materials as an attachment with the the send_as_attachment field is set to 1" do 
    it = Material.create(@materials_hash.merge(:send_notification => "1", :send_as_attachment => "1")) 
    Delayed::Worker.new(:quiet => true).work_off 
    email = ActionMailer::Base.deliveries[0] 
    email.parts.each.select{ |email| email.body =~ Regexp.new("Name of the posted material: " + it.name)}.should_not be_empty 
    email.has_attachments?.should be_true 
    end 

、私は、電子メールの各部分の体をチェックしなければなりませんでした、アタッチメントが最初のパートか2番目のパートかが一貫していなかったためです。

関連する問題