Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This has taken me the better part of 1-2 weeks to figure out, so hopefully this code will give you some idea about what to do. </p> <p>Here's my Lists controller where I generate the message to be sent to the mailer: </p> <pre><code>def messages @list = List.find(params[:id]) #@message = Message.new( :user_id =&gt; @list.user.id, :list_id =&gt; @list.id, #:subject =&gt; params[:subject], :body =&gt; params[:body] ) @message = Message.new(params[:message]) @contacts = @list.contacts respond_to do |format| if @list.save &amp;&amp; @message.save #Blaster is my mailer, and blast is the send method in the blaster.rb mailer Blaster.blast(@message, @contacts) format.html { redirect_to @list, notice: 'Message was saved.' } format.json { render json: @list, status: :created, location: @list } else format.html { redirect_to @list, notice: 'There was an error, try again'} format.json { render json: @list.errors, status: :unprocessable_entity } end end </code></pre> <p>end</p> <p>=========</p> <p>Here's my mailer - it's called blast.rb </p> <pre><code>def blast(message, contacts) # can't send without a message, and an array of contacts @message = message @contacts = contacts # with variables set, let's create the loop to do its magic @contacts.each do |contact| mail = mail( :to =&gt; "#{contact.email}", :from =&gt; "noreply@foo.org", :return_path =&gt; "noreply@foo.org", :subject =&gt; @message.subject, :body =&gt; @message.body #:template_path =&gt; 'blaster', #:template_name =&gt; 'blast' ) do |format| format.html { render 'blast.html.erb'} format.text { render 'blast.text.erb'} end mail.deliver end # contacts.each loop end #blast method </code></pre> <p>I'm pretty sure this code is sub-optimal and of poor quality, so caveat emptor, but perhaps you'll find something in here that'll help you solve your unique implementation. Hope it helps! </p> <p>D. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload