在ActionMailer中包含application_helper.rb

时间:2016-12-01 21:37:52

标签: ruby-on-rails ruby ruby-on-rails-4

我正在尝试访问我邮件中的一些application_helper方法,但这些SO帖子似乎没有任何效果:

View Helpers in Mailers

Access Helpers from mailer

app/helpers/application_helper.rb我有以下内容:

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

app/mailers/user_notifier.rb的邮寄地址中,我有以下内容:

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

在我的邮件中,我已经尝试了上述SO帖子中的所有建议,但我仍然收到此错误:

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

我目前正在使用Rails 4.1.7。

那么我需要做些什么才能在邮件程序中使用我的application_helper方法呢?

1 个答案:

答案 0 :(得分:0)

您可以尝试执行以下操作:

mailer app/mailers/user_notifier.rb

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

或者你可以试试这个:

helper ApplicationHelper