为什么“Action Mailer”没有呈现电子邮件模板?

时间:2013-06-13 21:07:05

标签: ruby ruby-on-rails-3 templates actionmailer

我在Ruby on Rails应用程序中使用“Action Mailer”来发送电子邮件。我有以下动作邮件:

class SecurityUserMailer < ActionMailer::Base

  default from: 'myemail@gmail.com'

  def password_reset(security_user)
    @security_user = security_user
    mail to: security_user.email, subject: 'Password Reset'
  end

  def email_confirmation(security_user)
    @security_user = security_user
    mail to: security_user.email, subject: 'Account Created'
  end

end

我正在成功发送电子邮件,但第二种方法(email_confirmation)未使用相应的模板。

电子邮件模板位于views / security_users_mailer文件夹中,其名称如下:

  1. email_confirmation.txt.erb
  2. password_reset.txt.erb
  3. 为什么只使用password_reset模板?

    请注意,首先,我认为我的模板中的代码可能有问题但后来我用文本内容替换它,并且不会再次渲染。

3 个答案:

答案 0 :(得分:4)

问题是由文件扩展名TYPO引起的。我有

  

email_confirmation。的 TXT .erb

并且邮件程序模板的扩展名应为文字 html

official docs看到 - 默认情况下,如果存在,则使用具有相同邮件程序操作的模板。

答案 1 :(得分:3)

Rails 4 我遇到了同样的问题,但我的问题是layouts/mailer.html.erb

中的空布局造成的

答案 2 :(得分:2)

我相信另一种方法是指定您想要呈现的模板以下是您可以如何进行此操作的示例

class SecurityUserMailer < ActionMailer::Base
  default from: 'myemail@gmail.com'
  def password_reset(security_user)
    @security_user = security_user
    mail to: security_user.email, subject: 'Password Reset'
  end

  def email_confirmation(security_user)
    @security_user = security_user
    mail (:to =>  security_user.email, 
          :subject => 'Account Created', 
          :template_path => 'email_confirmation.txt.erb',
          :template_name => 'another')
  end
end

看看下面应该提供一些进一步的见解:

Mailer Views

或者看看

Api Ruby on Rails您会看到Or even render a special view所示的示例,以便您可以在mail块中包含以下内容:

mail (:to =>  security_user.email, 
              :subject => 'Account Created') do |format|
               format.html { render 'another_template' }
               format.text { render :text => 'email_confirmation.txt.erb' }
      end

这应该说明你想要完成的事情