【问题标题】:Why "Action Mailer" is not rendering the email template?为什么“Action Mailer”不呈现电子邮件模板?
【发布时间】:2018-03-21 14:47:14
【问题描述】:

我在 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

为什么只使用 password_reset 模板?

请注意,首先,我认为模板中的代码可能有问题,但后来我将其替换为文本内容,并且不再呈现。

【问题讨论】:

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


    【解决方案1】:

    Rails 4 我遇到了同样的问题,但我的问题是由于 layouts/mailer.html.erb 中的布局为空而引起的

    【讨论】:

    • 当然。事后看来,看到你的回答让我的问题变得如此明显。
    【解决方案2】:

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

    email_confirmation.txt.erb

    邮件模板的扩展名应为 texthtml

    正如您从official docs 中看到的那样 - 如果存在,则默认使用具有相同邮件操作的模板。

    【讨论】:

      【解决方案3】:

      我相信另一种方法是指定您要呈现的模板以下是您可以如何处理的示例

      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
      

      这应该可以说明您要完成的工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-21
        • 2015-08-23
        • 2022-01-20
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        • 2020-09-18
        相关资源
        最近更新 更多