[英]Rails Action Mailer Issue
我正在尝试使用 ActionMailer(Rails 默认邮件程序上的 Ruby)将 HTML 代码作为变量发送。
我有一个带有简单变量的示例(这也是 HTML 代码)
def newsletter // ==> my method to send
@template = "<h1>wait what ?</h1>"
mail(to: "email@test.com", subject: "test de newsletter")
end
newsletter.html.erb // ==> my html.erb file
<%= @template %>
<h1>wait what ?</h1>
我得到的结果是:
<h 1 > wait what ? </ h 1> // ==> cannot write h1 here tho
<b>wait what ?</b>
// ==> in the terminal the mail looks like this
<body>
<h1>wait what ?;/h1>
<h1>wait what ? </h1>
</body>
也许我可以上传整个文件作为模板? 如何在我的变量中写入 HTML 代码?
也许我可以上传整个文件作为模板?
是的,您可以使用整个文件作为模板。 它被称为视图。 请参阅下面的参考资料。
如何在我的变量中写入 HTML 代码? 使用
raw
方法:
<%= raw @template %>
您可能想尝试这个ActionMailer walkthrough 。
参考: 创建邮件程序视图
在您的视图文件 (newsletter.html.erb) 中将字符串标记为可信安全。
<%= @template.html_safe %>
如果字符串有可能为 nil,则使用 raw。
<%= raw(@template) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.