【发布时间】:2010-06-16 15:16:03
【问题描述】:
我正在构建一个电子邮件系统,将我的不同电子邮件存储在数据库中,并通过method_missing 调用适当的“deliver_”方法(因为我无法明确声明方法,因为它们是用户生成的)。
我的问题是我的 rails 应用程序仍然尝试为生成的电子邮件呈现模板,尽管这些模板不存在。我想强制所有电子邮件使用相同的模板 (views/test_email.html.haml),该模板将被设置为从我的数据库记录中提取它们的格式。
我怎样才能做到这一点?我尝试在emailer_controller 的test_email 方法中添加render :template => 'test_email',但没有成功。
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
我在上面遇到的错误:
模板丢失
缺少模板 emailer/name_of_email_in_database.erb 在视图中 路径应用程序/视图
【问题讨论】:
标签: ruby-on-rails email