如何使用我的视图助手在我的ActionMailer视图?

我想在ReportMailer视图(app/views/report_mailer/usage_report.text.html.erb)中使用我在app/helpers/annotations_helper.rb中定义的方法。我怎么做呢?

基于本指南,似乎add_template_helper(helper_module)方法可以做我想做的事情,但我不知道如何使用它。

(顺便说一句,你在邮件视图中访问不同的一组助手有什么原因吗?这很烦人。)

66901 次浏览

在您用于管理电子邮件的mailer类中:

class ReportMailer < ActionMailer::Base
add_template_helper(AnnotationsHelper)


...
end

在Rails 3中,只需使用ActionMailer类顶部的helper方法:

helper :mail   # loads app/helpers/mail_helper.rb & includes MailHelper

我只是传入了一个block,因为我只需要它在一个Mailer中:

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中的所有邮件(设置"application" helper):

# config/application.rb:
...
config.to_prepare do
ActionMailer::Base.helper "application"
end

你可以添加你的邮件

helper :application

或者任何你需要的帮手

对于Ruby on Rails 4,我必须做两件事:

(1)正如杜克大学已经说过的,例如,如果你想添加的helper是UsersHelper,那么添加

helper :users

到派生的ActionMailer类(例如app/mailers/user_mailer.rb)

在那之后,我得到了一个新的错误:

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,将localhost替换为生产助手生成的url的更合适的主机。


问:对于#2,为什么邮件视图需要这些信息,而常规视图不需要?

答:因为常规视图不需要知道host,因为所有生成的链接都是由它们链接到的主机提供的。在电子邮件中出现的链接不是来自同一个主机(除非你链接到hotmail.comgmail.com等)。

在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

(这是一个老问题,但Rails已经发展了,所以我分享了在Rails 5.2中对我有用的东西。)

通常情况下,您可能希望使用自定义视图助手来呈现电子邮件的主题行以及HTML。在视图助手位于应用程序引入application_helper . rb之后/帮手的情况下,如下所示:

module ApplicationHelper


def mydate(time, timezone)
time.in_time_zone(timezone).strftime("%A %-d %B %Y")
end


end

我可以创建一个动态电子邮件主题行和模板,它们都使用helper,但我需要告诉Rails以两种不同的方式显式地在应用程序/梅勒/ user_mailer.rb中使用ApplicationHelper,正如你在这里的第二和第三行中看到的那样:

class UserMailer < ApplicationMailer


include ApplicationHelper  # This enables me to use mydate in the subject line
helper :application  # This enables me to use mydate in the email template (party_thanks.html.erb)


def party_thanks
@party = params[:party]
mail(to: 'user@domain.com',
subject: "Thanks for coming on #{mydate(@party.created_at, @party.timezone)}")
end


end

我想说的是,这两条线的效果是一样的,所以选择其中一条:

helper :application


add_template_helper(ApplicationHelper)

FWIW,在app / views / user_mailer / party_thanks.html.erb的电子邮件模板看起来像这样:

<p>
Thanks for coming on <%= mydate(@party.created_at, @party.timezone) %>
</p>

应用程序/控制器/ party_controller.rb控制器看起来像这样

class PartyController < ApplicationController
...
def create
...
UserMailer.with(party: @party).party_thanks.deliver_later
...
end
end

我不得不同意OP (@Tom Lehman)和@gabeodess的观点,考虑到https://guides.rubyonrails.org/action_mailer_basics.html#using-action-mailer-helpers,这一切都感觉相当复杂,所以也许我遗漏了一些东西……

这就是我在rails 6中所做的

class ApplicationMailer < ActionMailer::Base
default from: 'community@example.com'
layout 'mailer'


# Add whatever helper you want
helper :application


end