Rails: Custom template for email "deliver_" method?

Posted by neezer on Stack Overflow See other posts from Stack Overflow or by neezer
Published on 2010-06-16T15:16:03Z Indexed on 2010/06/16 15:42 UTC
Read the original article Hit count: 409

Filed under:
|

I'm building an email system that stores my different emails in the database and calls the appropriate "deliver_" method via method_missing (since I can't explicitly declare methods since they're user-generated).

My problem is that my rails app still tries to render the template for whatever the generated email is, though those templates don't exist. I want to force all emails to use the same template (views/test_email.html.haml), which will be setup to draw their formatting from my database records.

How can I accomplish this? I tried adding render :template => 'test_email' in the test_email method in emailer_controller with no luck.


models/emailer.rb:

class Emailer < ActionMailer::Base

  def method_missing(method, *args)
    # not been implemented yet
    logger.info "method missing was called!!"
  end

end

controller/emailer_controller.rb:

class EmailerController < ApplicationController

  def test_email
    @email = Email.find(params[:id])
    Emailer.send("deliver_#{@email.name}")
  end

end

views/emails/index.html.haml:

%h1 Listing emails
%table{ :cellspacing => 0 }
  %tr
    %th Name
    %th Subject
  - @emails.each do |email| 
    %tr
      %td=h email.name
      %td=h email.subject
      %td= link_to 'Show', email
      %td= link_to 'Edit', edit_email_path(email)
      %td= link_to 'Send Test Message', :controller => 'emailer', :action => 'test_email', :params => { :id => email.id }
      %td= link_to 'Destroy', email, :confirm => 'Are you sure?', :method => :delete
%p= link_to 'New email', new_email_path

Error I'm getting with the above:

Template is missing

Missing template emailer/name_of_email_in_database.erb in view path app/views

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about email