Forum: Ruby on Rails How to apply a specific "view" when I send out an *email*

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-03-04 16:17
Hi there,

how can I tell my Rails app Mailer to:

-> Apply a specific "view" when I send out an *email* ?

When I don't specify anything specific, my "WhateverMailer" looks for
its "view" (.erb file) as if the Mailer was a Controller (following the
same standard logic: ../app/views/ directory).

But I need my WhateverMailer to go find the .erb file on a different
path - but how and where to tell it to??

Thanks a lot for any help!
Tom
988320371d9a18f0d50375188e01d54a?d=identicon&s=25 Peter Vandenabeele (Guest)
on 2009-03-04 17:03
(Received via mailing list)
On Wed, Mar 4, 2009 at 4:17 PM, Tom Ha
<rails-mailing-list@andreas-s.net> wrote:
> how can I tell my Rails app Mailer to:
>
> -> Apply a specific "view" when I send out an *email* ?

The trick is

    body       render_message(channel, "")

in app/models/..._mailer.rb   (example below).

So, a few hints from my code (changed some parts for censuring
the internal business logic, so may be inexact, sorry upfront for that)

In the app/controllers/mail_controller.rb

def handle_email
...
    case preview_or_send
    when :preview
      @email = ChannelMailer.create_channel_mail(details, @channel)
    when :send
      delivery_state = "OK"
      begin
        @email = ChannelMailer.deliver_channel_mail(details, @channel)
      rescue
        delivery_state = "FAIL"
        logger.info "FAILED mail delivery, email = #{@email}"
      end
      ...
      fill_log_entry @email, delivery_state, details, @channel
    end
...
end

In app/models/channel_mailer.rb
class ChannelMailer < ActionMailer::Base
  def channel_mail(details, channel)
    subject    "censured"
    from       FROM
    sent_on    Time.now

    case channel
    when "censured"
        to_list = ...
        bcc_list = ...
    ...
    end
    set_recipients(to_list, bcc_list)
    body       render_message(channel, "") # ###### This is the real
answer to your question
...
  end
...
end

In app/view/channel_mailer/
$ ls -1
_details_for_channel.erb
channel_1.erb
channel_2.erb
channel_3.erb


HTH,

Peter
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-03-04 17:28
Very cool - thanks a lot, Peter!
This topic is locked and can not be replied to.