如何从ActionMailer预览访问视图助手或view_context?

时间:2017-07-26 21:56:57

标签: ruby-on-rails model-view-controller ruby-on-rails-5

如何从ActionMailer :: Preview访问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
  

未定义的局部变量或方法`view_context'用于#&lt; EventMailerPreview:0x0000001567a448&gt;

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.

1 个答案:

答案 0 :(得分:0)

您应该可以直接访问它们: 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