首页 > 解决方案 > 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 元素。

谢谢你的帮助

标签: ruby-on-railsactionmailer

解决方案


如果我已经理解您要正确执行的操作,您可以使用布局来执行此操作。这与使用 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>

推荐阅读