Dynamic actions and views


I have a set of messages stored in the db. These are email messages
that I have to send based on some condition. This is not the way I had
been doing all these days. I used to create an action in my controller
(class Notifier < ActionMailer::ARMailer) and then create an rhtml
file with the email content and then say
Notifier::deliver_my_email_message to send out the emails. But now
that I am storing these messages in the database, is there a way I can
define the controller action and view dynamically and then pass the
message as a parameter?

Or is there a better way to handle this?

If you want to send emails which are stored in your database, you can
something similar to the following:

class MyMailer < ActionMailer::Base
def from_database(email)
@subject = email.subject
@to = email.recipient
@from = email.from_address
@body = email.body

class MyController < ApplicationController
def send_email
email = EmailTemplate.find_by_id([:id])
mailer = MyMailer.deliver_from_database(email)

This is old code and I haven’t done this in a while, so there might be a
better way to do it now.


Thanks much Adam. This makes sense.