2016-03-29 30 views
1

Birden çok markete birden fazla e-posta göndermeye çalışıyorum. Posta alıcısının e-postasını değiştirirken birden çok kez çalışan bir döngü oluşturmaya çalıştım ancak yalnızca son posta isteğine gönderilecek. Bunu çözmek için nasıl giderim? İşte benim geçerli kod:raylar birden fazla e-posta gönderiyor mu?

@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 

belli markanıza ait üzerinde ürünler elde etmek için My html kodu: Ben döngü koştu ve onu koşarken

<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> 

benim e-posta için ayıklayıcı var her döngüde mail(to: @brands[count].email, subject: 'A purchase has been made!') hattı neden bu yüzden çalışmadığını anlamakta başarısız olur.

+0

Doğru hatırlıyorsam, posta nesnesinin bir gönderme yöntemi vardır Olup bitenler, tüm bu postaları etkili bir şekilde oluşturuyorsunuz, ancak raylar sadece sizin için son olanı gönderir. en son döndü – Sidewinder94

+0

Ah görüyorum Bu eylemi gerçekleştirmenin daha iyi bir yolu olur mu? –

cevap

1

Sen resmi örnekte olduğu gibi postasının dışında döngü yapmalıdır: Burada açıklanan

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

olarak: http://guides.rubyonrails.org/action_mailer_basics.html ve muhtemelen deliver_now kısmını eksik (deliver_later yerini ve işçi tarafından yapılabilir process

İlgili konular