# Dynamic actions and views

**URL:** <https://www.ruby-forum.com/t/dynamic-actions-and-views/132122>\
**Category:** Rails\
**Created:** [March 17, 2008, 7:06am UTC](https://www.ruby-forum.com/t/dynamic-actions-and-views/132122 "2008-03-17T07:06:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [March 17, 2008, 7:06am UTC](https://www.ruby-forum.com/t/dynamic-actions-and-views/132122/1 "2008-03-17T07:06:01Z")

</div>

Hi,

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?

---

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [March 17, 2008, 7:25am UTC](https://www.ruby-forum.com/t/dynamic-actions-and-views/132122/2 "2008-03-17T07:25:57Z")

</div>

If you want to send emails which are stored in your database, you can  
use  
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  
end  
end

class MyController \< ApplicationController  
def send\_email  
email = EmailTemplate.find\_by\_id([:id])  
mailer = MyMailer.deliver\_from\_database(email)  
end  
end

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

Adam

---

<div class="post-metadata">

**Author:** ![Love\_AJAX](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/779978/32.png) [@Love\_AJAX](https://www.ruby-forum.com/u/Love_AJAX)\
**Post date:** [March 17, 2008, 8:51am UTC](https://www.ruby-forum.com/t/dynamic-actions-and-views/132122/3 "2008-03-17T08:51:14Z")

</div>

Thanks much Adam. This makes sense.  
-subbu
