2016-03-29 5 views
1

複数のメールを複数のブランドに送信しようとしています。私はメール受信者の電子メールを変更しながらこの複数回実行するループを作成しようとしましたが、最後のメール要求にのみ送信します。これをどうやって解決するのだろうか?ここに私の現在のコードは次のとおりです。複数のメールを送信していますか?

@user_products.each do |p| 
    @brands << p.brand 
end 
count = 0 
@brands = @brands.uniq 
while count < @brands.count 
    debugger 
     @brand = @brands[count] 
      mail(to: @brands[count].email, subject: 'A purchase has been made!') 
      count += 1 
    end 

私のhtmlコードは、特定のブランドで作られた上で製品を得るために:私はループを実行し、それが走ったとして私の電子メールのためのデバッガがある

<body style="margin-left: auto; margin-right: auto; background-color: #d55d5d; width: 75%;"> 
    <div align="center"> 
     <img src="http://localhost:3000/assets/motobanner-d2fde8a6e30060905a6f72b0c8128d222a7596e690a7a4c648e5e81109bf4600.jpg" style="margin-top: 0% ;width: 75%; height: 240px;" ></img> 
    </div> 
    <div align="center"> 
     <h2>Hello, <%= @brand.name %>!</h2> 
      <p style="font-size: 1.2em;">There has been a purchase of the following items:</p> 
      <% @user_products.each do |item| %> 
       <div align="center"> <%= image_tag "http://localhost:3000/#{item.picture.url}", alt: item.product_name if item.picture && item.brand_id == @brand.id %><br /></div> 
       <b><%= "Product name:" if item.brand_id == @brand.id %></b> <%= item.product_name if item.brand_id == @brand.id %> 
       <b><%= "Product description:" if item.brand_id == @brand.id %></b><br /> <p class="product-description"><%= item.product_description if item.brand_id == @brand.id %></p> 
       <b><%= "Product price:" if item.brand_id == @brand.id %></b> <%= "£" + item.product_price.to_s if item.brand_id == @brand.id %> 
      <% end %> 
</body> 

なぜなら、なぜそれがうまくいかないのか理解できない理由です。

+0

メールオブジェクトはsendメソッドを持っています。 あなたが効果的にすべてのメールを構築していると思いますが、レールが最後のものを送信しますそれは最後に返されます。 – Sidewinder94

+0

ああ私が見るこのアクションを実行するためのより良い方法がありますか? –

答えて

1

あなたは公式の例のように、メーラ外ループ実行する必要があります。ここで説明する

class SendWeeklySummary 
    def run 
    User.find_each do |user| 
     UserMailer.weekly_summary(user).deliver_now 
    end 
    end 
end 

として:http://guides.rubyonrails.org/action_mailer_basics.html を、あなたはおそらくdeliver_nowの一部が欠落している(deliver_laterによって置き換えられ、作業者が行うことができますプロセス

関連する問題