如何从操作邮件程序预览访问视图帮助程序或view_context?



您如何从ActionMailer::P review访问view_context?

class EventMailerPreview < ActionMailer::Preview
def notify_user
EventMailer.notify_user user: stripe_detail.subscription.user, 
plan: event.data.object.lines.data[0].plan.name,
amount: view_context.number_to_currency(event.data.object.total / 100.0, locale: :us),
date: Time.at(event.data.object.lines.data[0].period.start).to_date

#<EventMailerPreview:0x0000001567a448> 的未定义局部变量或方法 'view_context'

http://guides.rubyonrails.org/action_mailer_basics.html#previewing-emails

http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_currency

我也尝试过没有view_context,我也尝试了helpers..

您应该能够直接访问它们: http://guides.rubyonrails.org/action_mailer_basics.html#using-action-mailer-helpers

你使用的是哪个版本的 Rails?在版本 5.0 及更早版本中,您可能必须显式声明所需的帮助程序:

class EventMailerPreview < ActionMailer::Preview
helper ApplicationHelper
def notify_user
# ...
end
end

最新更新