ruby on rails - How to stop from view being rendered while sending email -


i using rails 3.0.9 , have following code send email when comment posted.

class mailer < actionmailer::base    def comment_notification(comment)     user.active.each |user|       @user = user       mail(:to => @user.email, :subject => subject)     end   end  end 

if there not active users user.active empty , code inside not executed. view rendered , view fails because @user missing.

the above code invoked observer

mailer.comment_notification(comment).deliver 

one way fix problem change code in observer this

user.active.each |recipient|   mailer.comment_notification(comment, recipient).deliver end 

is right way fix way. observer thin possible.

yes, observer fix correct. should loop through , send emails 1 one. mailer should send 1 email @ time. job best left delayed job though. don't want waiting around while email sends.

here tutorial on delayed job: http://railscasts.com/episodes/171-delayed-job

be sure check readme delayed job well, paying special attention "rails 3 mailers" section: http://github.com/collectiveidea/delayed_job


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -