1

I'm trying to get access to some of my application_helper methods within my mailer, but nothing seems to be working from these SO posts:

View Helpers in Mailers

Access Helpers from mailer

In app/helpers/application_helper.rb I have the following:

module ApplicationHelper
  def get_network_hosts
    .. stuff get get a @network_hosts object
  end
end

In my mailer at app/mailers/user_notifier.rb I have the following:

class UserMailer < ActionMailer::Base
  default from: "Support <support@me.co>"
  add_template_helper(ApplicationHelper)

  def trial_notifier(user)
    get_network_hosts
    @user = user
    @total = user.company.subscription.per_device_charge * @network_hosts.count
    if @total < 501
      @message = "You'd pay #{@total}/month if you converted to full-access now!"
    else
      @message = "You'd pay #{@total}/month if you converted to full-access now, but we have a better deal for you!"
    end

    @url = edit_user_registration_url
    mail(to: @user.email, subject: 'What's up?')
  end
end

In my mailer I've tried all of the suggestions in the above SO posts, but I'm still getting this error:

 `NameError: undefined local variable or method `get_network_hosts' for #<UserMailer:0x007fe756c67c18`>

I'm currently using Rails 4.1.7.

So what do I have to actually do to be able to use my application_helper methods within a mailer?

Community
  • 1
  • 1
Godzilla74
  • 2,358
  • 1
  • 31
  • 67

1 Answers1

1

You can try to do this as following:

In your mailer at app/mailers/user_notifier.rb:

class UserMailer < ActionMailer::Base
  default from: "Support <support@me.co>"
  helper :application

or you can try this:

helper ApplicationHelper
Khanh Pham
  • 2,848
  • 2
  • 28
  • 36