ruby-on-rails – Action Mailer:如何在存储在数据库中的电子邮
发布时间:2020-12-17 04:03:23 所属栏目:百科 来源:网络整理
导读:我有Action Mailer设置来使用我的电子邮件模型的body属性(在数据库中)呈现电子邮件.我希望能够在身体中使用erb,但我无法弄清楚如何在发送的电子邮件中呈现它. 我可以使用此代码将正文作为字符串 # models/user_mailer.rbdef custom_email(user,email_id) ema
我有Action Mailer设置来使用我的电子邮件模型的body属性(在数据库中)呈现电子邮件.我希望能够在身体中使用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,它说我可以使用渲染但我只能得到渲染:文本工作而不渲染:内联 # 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 更新:有人建议在此线程http://www.ruby-forum.com/topic/67820上使用initialize_template_class.我现在有这个用于正文 body :msg => initialize_template_class(:user => user).render(:inline => email.body) 它有效,但我不明白这一点,所以我尝试研究私有方法,并没有太多的东西让我担心这是一个黑客,可能有更好的方法.建议? 解决方法
即使你最终无法使用render:inline,你也可以自己实例化ERb.
require 'erb' x = 42 template = ERB.new <<-EOF The value of x is: <%= x %> EOF puts template.result(binding) #binding here is Kernel::binding,the current variable binding,of which x is a part. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |