尝试通过Rails ActionMailer发送一封电子邮件会导致调用'lookup_context.rb`内的`normalize_name`失败
我有一个继承自ApplicationMailer的Mailer类,它继而继承自ActionMailer :: Base。 Ruby版本是ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]
。尝试通过Rails ActionMailer发送一封电子邮件会导致调用'lookup_context.rb`内的`normalize_name`失败
的梅勒类如下所示:
class PurchaseOrderStatusMailer < ApplicationMailer
CONTACTS = {
JC: ['[email protected]'],
RM: ['[email protected]']
}
def daily_report_email(facility)
@facility = facility
ingredient_items = LineItem.ingredient.by_facility(facility)
@delivered_count = ingredient_items.by_date([7.days.ago, 7.days.from_now]).delivered.count
@partial_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).partial.count
@late_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).late.count
@expected_count = ingredient_items.by_date([Date.today, 7.days.from_now]).expected.count
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}")
end
end
ApplicationMailer如下所示:
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: '[email protected]'
def facility_email(facility)
emails = Rails.application.config_for(:emails)
(emails[facility] + emails["DEFAULT"]).flatten
end
end
视图位于app/views/purchase_order_status_mailer/daily_report_email.html.erb
。
当我打开我的Rails控制台,输入PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver
,我看到以下错误:
NoMethodError: undefined method `empty?' for nil:NilClass
from /Users/me/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionview-5.0.4/lib/action_view/lookup_context.rb:215:in `normalize_name'
我想传递一个format
块的调用mail
助手用同一调用,就像这样:
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}") do |format|
format.text { render plain: "Hey!" }
end
上面产生了以下的反应,这似乎代表一个成功的电子邮件发送:
Rendering text template
Rendered text template (0.0ms)
Sent mail to [email protected] (8.8ms)
Date: Mon, 25 Sep 2017 12:55:11 -0400
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hey!
=> #<Mail::Message:70191029273300, Multipart: false, Headers: <Date: Mon, 25 Sep 2017 12:55:11 -0400>, <From: [email protected]>, <To: ["[email protected]"]>, <Message-ID: <[email protected]>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
我实际上没有收到电子邮件,我假设这意味着我没有在本地计算机上设置SMTP,但上述响应令人鼓舞。除了我上面发布的错误之外,没有任何堆栈跟踪,所以我尝试挖掘Rails源代码,并且我看到在args_for_lookup
受保护的方法内lookup_context.rb
内部的normalize_name
被调用,而受保护的方法又被ViewPaths
模块的find_all
方法调用。但除此之外,很难追踪调用堆栈,因为我找不到调用者是谁。
我的问题是:第一次打电话给mail
有什么问题?
编辑1:我也尝试format.html { render html: "<h1>Hello Mikel!</h1>".html_safe }
而不是format.text
选项,根据示例here,我收到了类似的成功消息。
然后,我尝试在normalize_name
内添加一个byebug语句,尝试确定参数的值是否成功发送到电子邮件,但看起来像这个方法在块传递时没有被调用。这使我更加强烈地怀疑,这个问题与我的观点有某种关系。但我还不能确认。
我通过传递template_path
和template_name
选择原来的调用mail
帮手,像这样能够产生success
响应(包括我的邮件呈现的模板文件):
mail(
to: CONTACTS[facility.to_sym],
subject: "#{facility} Daily Receipt Status - #{Date.today}",
template_path: 'purchase_order_status_mailer',
template_name: 'daily_report_email')
这产生以下成功回应:
Rendering purchase_order_status_mailer/daily_report_email.text.erb
Rendered purchase_order_status_mailer/daily_report_email.text.erb (0.3ms)
Sent mail to [email protected] (12.1ms)
Date: Mon, 25 Sep 2017 15:18:25 -0400
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_59c9568120033_11e943fee8503f8282341e
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Please find the receipt status for JC as of 09-25-2017:
=================================================================================================
...<lots more content, including ERB rendered into HTML>...
=> #<Mail::Message:70293683934580, Multipart: true, Headers: <Date: Mon, 25 Sep 2017 15:18:25 -0400>, <From: [email protected]>, <To: ["[email protected]"]>, <Message-ID: <[email protected]>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
我有点困惑,为什么我不得不添加这些选项,如the documentation不说清楚,这些都需要nd我的模板和文件夹似乎正确命名,但它的工作,我有截止日期,所以我继续前进。 :-)