ruby-on-rails - Rails 邮件程序 append_view_path 不起作用
问题描述
我正在尝试使用append_view_path
以在发送的每封电子邮件的末尾呈现包含我想要的一些额外信息的部分。
我的邮件程序类如下所示:
class PasswordMailer < ActionMailer::Base
default :from => CaseCenter::Config::Reader.get('email_from')
append_view_path Rails.root.join('app','views','password_mailer')
def password_changed(user)
@user = user
mail(:to => user.email, :subject => t('mailer.email_topic_password_changed'))
end
end
当它运行时,它不会将我的部分添加到电子邮件的末尾。(虽然发送了一封电子邮件)。我的部分位于app/views/password_mailer/password_changed_template.html.erb
并且只包含一个简单的 HTML 元素。
谢谢你的帮助
解决方案
如果我已经理解您要正确执行的操作,您可以使用布局来执行此操作。这与使用 ActionController 进行 MVC 时使用的布局非常相似。
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
layout 'mailer'
end
class PasswordMailer < ApplicationMailer
default from: CaseCenter::Config::Reader.get('email_from')
def password_changed(user)
@user = user
mail(to: user.email, subject: t('mailer.email_topic_password_changed'))
end
end
应用程序/视图/布局/mailer.html.erb:
<!DOCTYPE html>
<html lang="en">
<head>
# ...
</head>
<body>
<%= yield %>
<hr>
<footer>
This spam was sent to you by EvilCorp. If you where looking to unsubscribe you are out of luck.
</footer>
</body>
</html>
应用程序/视图/布局/mailer.txt.erb:
<%= yield %>
-------------------------------------
This spam was sent to you by EvilCorp.
If you where looking to unsubscribe you are out of luck.
当然,您也可以将页脚分成部分并像在普通视图中一样渲染它:
<!DOCTYPE html>
<html lang="en">
<head>
# ...
</head>
<body>
<%= yield %>
<%= render 'shared/mailer_footer' %>
</body>
</html>
推荐阅读
- r - 在for循环中循环n页 - R
- react-native - 如何使用 react native 制作 vpn 应用程序
- bash - Increment version with 1 bash (awk)
- node.js - 如何从 node.js 中的发布请求发送响应?
- javascript - 如何在nodejs api调用上调用完整方法
- vue.js - 检测 vuex 状态变化以在 nuxt 布局内执行方法
- ios - 在 TableView 中选择单元格时获取标签文本(Swift)
- android - 购物车页面崩溃 FirebaseRecyclerAdapter 问题
- php - 使用 PHP 查询程序 firebird 数据库
- apache-flink - Apache Flink Stateful Functions 状态缩放