我有 Action Mailer 设置以使用我的电子邮件模型的正文属性(在数据库中)呈现电子邮件。我希望能够在正文中使用 erb,但我不知道如何让它在发送的电子邮件中呈现。
我可以使用此代码将正文作为字符串
# models/user_mailer.rb
def custom_email(user, email_id)
email = Email.find(email_id)
recipients user.email
from "Mail It Example <admin@foo.com>"
subject "Hello From Mail It"
sent_on Time.now
# pulls the email body and passes a string to the template views/user_mailer/customer_email.text.html.erb
body :msg => email.body
end
我遇到了这篇文章http://rails-nutshell.labs.oreilly.com/ch05.html说我可以使用render
但我只能render :text
上班而不是render :inline
# models/user_mailer.rb
def custom_email(user, email_id)
email = Email.find(email_id)
recipients user.email
from "Mail It Example <admin@foo.com>"
subject "Hello From Mail It"
sent_on Time.now
# body :msg => email.body
body :msg => (render :text => "Thanks for your order") # renders text and passes as a variable to the template
# body :msg => (render :inline => "We shipped <%= Time.now %>") # throws a NoMethodError
end
更新:有人推荐initialize_template_class
在这个线程上使用http://www.ruby-forum.com/topic/67820。我现在有这个body
body :msg => initialize_template_class(:user => user).render(:inline => email.body)
它有效,但我不明白这一点,所以我尝试研究私有方法,但那里没有太多东西,这让我担心这是一个黑客,可能有更好的方法。 建议?