首页 > 解决方案 > Rails 6.0.2 ActionMailer 问题:找不到模板

问题描述

我在开发环境中。在 UI 中触发电子邮件时,我在浏览器中收到错误消息:

No template for interactive request
Admins::AdminsController#testmail is missing a template for request formats: text/html

NOTE!
Unless told otherwise, Rails expects an action to render a template with the same name,
contained in a folder named after its controller. If this controller is an API responding with 204 (No Content),
which does not require a template, then this error will occur when trying to access it via browser,
since we expect an HTML template to be rendered for such requests. If that's the case, carry on.

日志显示:

Started GET "/en/admins/1/testmail" for 127.0.0.1 at 2020-03-05 08:28:00 +0100
Processing by Admins::AdminsController#testmail as HTML
  Parameters: {"locale"=>"en", "admin_id"=>"1"}
  Rendering test_mailer/testmail.text.erb within layouts/mailer
  Rendered test_mailer/testmail.text.erb within layouts/mailer (Duration: 0.1ms | Allocations: 4)
TestMailer#testmail: processed outbound mail in 2.0ms
Delivered mail 5e60aa00a74a7_20213ffce2ad423c8083a@domain.example.com.mail (59.0ms)
Date: Thu, 05 Mar 2020 08:28:00 +0100
From: admin@domain.com
To: me@example.com
Message-ID: <5e60aa00a74a7_20213ffce2ad423c8083a@domain.example.com.mail.mail>
Subject: Welcome to My Awesome Site
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Welcome to example.com
===============================================
You have successfully signed up to example.com,
your username is: XXXX
To login to the site, just follow this link: .
Thanks for joining and have a great day!

Completed 406 Not Acceptable in 64ms (ActiveRecord: 0.0ms | Allocations: 3862)
ActionController::MissingExactTemplate (Admins::AdminsController#testmail is missing a template for request formats: text/html):
actionpack (6.0.2.1) lib/action_controller/metal/implicit_render.rb:45:in `default_render'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:196:in `process_action'

可以在上面看到模板是在错误发生之前呈现的。

我的邮件类:

class TestMailer < ApplicationMailer
  def testmail
    mail(to: 'user@domain.com',
         subject: 'Welcome to My Awesome Site') do |format|
      format.text
    end
  end
end

当然模板放在/app/views/test_mailer/testmail.text.erb.

我在 development.rb 中的邮件服务器配置是

我在 localhost 上运行了 mailcatcher

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false

config.action_mailer.smtp_settings = {
address: 'localhost',
port: 1025
}
config.action_mailer.delivery_method :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false

我在 Centos 7 上,整个 Rails 应用程序路径中没有特殊字符或空格 感谢您的调查。

标签: ruby-on-railsactionmailerruby-on-rails-6

解决方案


问题不是根据日志发送成功的邮件

问题是您的控制器操作 Admins::AdminsController#testmail

成功发送电子邮件时,视图中没有用于呈现任何 html 的模板

我想路径会是这样的:

/app/views/admins/admins/testmail.html.erb

推荐阅读