简体   繁体   中英

How do you access view helpers or view_context from an ActionMailer preview?

How do you access the view_context from ActionMailer::Preview?

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

undefined local variable or method `view_context' for #<EventMailerPreview:0x0000001567a448>

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

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

I also tried without view_context and I also tried helpers. .

You should be able to access them directly: http://guides.rubyonrails.org/action_mailer_basics.html#using-action-mailer-helpers

What version of Rails are you on? On versions 5.0 and earlier, you may have to explicitly declare the helpers that you need:

class EventMailerPreview < ActionMailer::Preview
    helper ApplicationHelper

    def notify_user
        # ...
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM