我的 Rails 应用程序中有这样的工作:
class NewAnswerNotifyJob < ApplicationJob
queue_as :default
def perform(answer)
Services::NewAnswerNotify.new.send_notify(answer)
end
end
Services::NewAnswerNotify
:
class Services::NewAnswerNotify
def send_notify(answer)
NewAnswerNotifyMailer.new.notify(answer)
end
end
NewAnswerNotifyMailer
:
class NewAnswerNotifyMailer < ApplicationMailer
def notify(answer)
@answer = answer
@question = answer.question
@author = answer.question.author
mail to: @author.email
end
end
当我尝试在 Rails 控制台中(我在开发服务器上遇到此问题,然后在控制台中重播此行为)以answer
运行Services::NewAnswerNotify#send_notify
时,我收到以下错误:
2.6.0 :023 > answer = Answer.first
Answer Load (0.5ms) SELECT "answers".* FROM "answers" ORDER BY "answers"."best_solution" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Answer id: 76, body: "answer body", question_id: 2, created_at: "2019-05-01 18:43:16", updated_at: "2019-05-28 15:38:16", author_id: 1, best_solution: true>
2.6.0 :024 > Services::NewAnswerNotify.new.send_notify(answer)
Question Load (0.6ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Traceback (most recent call last):
3: from (irb):24
2: from app/services/new_answer_notify.rb:3:in `send_notify'
1: from app/mailers/new_answer_notify_mailer.rb:8:in `notify'
NoMethodError (undefined method `humanize' for nil:NilClass)
2.6.0 :025 >
因此,错误发生在NewAnswerNotifyMailer
mail to: @author.email
行中,但是当邮件程序本身按计划工作时:
2.6.0 :025 > answer = Answer.first
Answer Load (0.7ms) SELECT "answers".* FROM "answers" ORDER BY "answers"."best_solution" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Answer id: 76, body: "for flexbox grid columns also means you can set th...", question_id: 2, created_at: "2019-05-01 18:43:16", updated_at: "2019-05-28 15:38:16", author_id: 1, best_solution: true>
2.6.0 :026 > NewAnswerNotifyMailer.notify(answer)
Question Load (0.5ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
[i18n-debug] en.new_answer_notify_mailer.notify.subject => nil
Rendering new_answer_notify_mailer/notify.html.slim within layouts/mailer
Rendered new_answer_notify_mailer/notify.html.slim within layouts/mailer (4.7ms)
Rendering new_answer_notify_mailer/notify.text.slim within layouts/mailer
Rendered new_answer_notify_mailer/notify.text.slim within layouts/mailer (3.5ms)
NewAnswerNotifyMailer#notify: processed outbound mail in 100.5ms
=> #<Mail::Message:70164808395160, Multipart: true, Headers: <From: from@example.com>, <To: codcore@gmail.com>, <Subject: Notify>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_5d0cac2d80291_b85a3fd081039fd052340"; charset=UTF-8>>
我不知道问题出在哪里,为什么我在Services::NewAnswerNotify
Nil
.
几个建议:
- 您应该直接使用 ActionMailer 类方法,而不是使用
new
实例化新的邮件程序。这可能是错误的根源 - 由于您的
NewAnswerNotify
嵌套在Services
下,因此使用根命名空间::NewAnswerNotifyMailer
也会减少歧义(有些人可能不同意我的观点,但我过去有很多根命名空间错误,我现在倾向于系统地使用::
前缀) - 当心类加载对
class Services::NewAnswerNotify
和module Services class NewAnswerNotification
的工作方式不同(关于此主题的许多现有问题)
module Services
class NewAnswerNotify
def send_notify(answer)
::NewAnswerNotifyMailer.notify(answer).deliver_now # instead of .new.notify
end
end
end
还有一些关于变量和英语的侧面评论
我宁愿使用
Services::NewAnswerNotification
NewAnswerNotificationMailer
def send_notification(answer)
或def notify(answer)
从长远来看,也许从维护代码库后的经验中得出的最后一条建议:更明确地说明您通知谁def notify_question_author_of_new_answer
,因为稍后您可能会有notify_question_subscribers_of_new_answer
或其他人可能需要通知(当然,这完全取决于您的商业模式, 随意忽略此评论)