如何在我的ActionMailer视图中使用我的视图助手?
我想在我的ReportMailer视图( app/views/report_mailer/usage_report.text.html.erb )中使用我在app/helpers/annotations_helper.rb中定义的方法。 我该怎么做呢? 
 基于这个指南 ,看起来像add_template_helper(helper_module)方法可能做我想要的,但我不知道如何使用它。 
(顺便说一下,有没有一个原因,你可以访问一个不同的助手在邮件视图?这是非常恼人的。)
在您用来pipe理电子邮件的邮件类中:
 class ReportMailer < ActionMailer::Base add_template_helper(AnnotationsHelper) ... end 
在Rails 3中,只需使用ActionMailer类顶部的帮助器方法即可:
 helper :mail # loads app/helpers/mail_helper.rb & includes MailHelper 
我只是通过一个块,因为我只需要在一个梅勒:
 helper do def host_url_for(url_path) root_url.chop + url_path end end 
(一定要设置config.action_mailer.default_url_options。)
(如果你使用url_for,一定要通过:only_path => false)
对于Rails 3中的所有邮件程序(设置“应用程序”助手):
 # config/application.rb: ... config.to_prepare do ActionMailer::Base.helper "application" end 
对于Ruby on Rails 4,我必须做两件事:
  (1)由于杜克已经说过,如果你想添加的助手是UsersHelper ,例如,然后添加 
 helper :users 
 到派生的ActionMailer类(例如app/mailers/user_mailer.rb ) 
(2)之后,我得到一个新的错误:
 ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true) 
要解决这个问题,请添加该行
 config.action_mailer.default_url_options = { :host => 'localhost' } 
 到每个config/environments/*.rb文件。 对于config/environments/production.rb ,将localhostreplace为生成帮助程序生成的url的更合适的主机。 
问:对于#2,为什么邮件视图需要这些信息,而普通视图不需要?
 答:由于常规视图不需要知道host ,因为所有生成的链接都是从它们链接到的主机提供的。 在电子邮件中显示的链接不是从同一主机提供的(除非您链接到hotmail.com或gmail.com等) 
你可以添加你的邮件
 helper :application 
或者任何你需要的帮手
在我的Rails4的情况下,我这样做:
 # app/mailers/application_mailer.rb class ApplicationMailer < ActionMailer::Base add_template_helper ApplicationHelper ... end 
和
 # app/mailers/user_mailer.rb class AccountMailer < ApplicationMailer def some_method(x, y) end end 
 所以你不必在任何地方指定add_template_helper 。