(이것은 오래된 질문이지만 Rails는 진화했기 때문에 Rails 5.2에서 나에게 맞는 것을 공유하고 있습니다.)
일반적으로 전자 메일의 제목 줄과 HTML을 렌더링 할 때 사용자 지정보기 도우미를 사용할 수 있습니다. 뷰 헬퍼가 app / helpers / application_helper.rb에있는 경우 다음과 같습니다.
module ApplicationHelper
def mydate(time, timezone)
time.in_time_zone(timezone).strftime("%A %-d %B %Y")
end
end
도우미를 사용하는 동적 전자 메일 제목 줄과 템플릿을 만들 수 있지만 여기 에서 두 번째와 세 번째 줄에서 볼 수 있듯이 앱 / 메일러 /user_mailer.rb 에서 명시 적으로 ApplicationHelper를 사용하도록 Rails에 알려야 합니다. :
class UserMailer < ApplicationMailer
include ApplicationHelper # This enables me to use mydate in the subject line
helper :application # This enables me to use mydate in the email template (party_thanks.html.erb)
def party_thanks
@party = params[:party]
mail(to: 'user@domain.com',
subject: "Thanks for coming on #{mydate(@party.created_at, @party.timezone)}")
end
end
이 두 줄은 잘 작동하므로 언급하십시오.
helper :application
add_template_helper(ApplicationHelper)
app / views / user_mailer / party_thanks.html.erb 의 이메일 템플릿 인 FWIW 는 다음과 같습니다.
<p>
Thanks for coming on <%= mydate(@party.created_at, @party.timezone) %>
</p>
그리고 app / controller / party_controller.rb 컨트롤러는 다음과 같습니다
class PartyController < ApplicationController
...
def create
...
UserMailer.with(party: @party).party_thanks.deliver_later
...
end
end
https://guides.rubyonrails.org/action_mailer_basics.html#using-action-mailer-helpers를 감안할 때 OP (@Tom Lehman) 및 @gabeodess에 동의해야합니다 . .