Ruby-on-rails 如何在我的 ActionMailer 视图中使用我的视图助手?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1416978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use my view helpers in my ActionMailer views?
提问by Tom Lehman
I want to use the methods I defined in app/helpers/annotations_helper.rbin my ReportMailer views (app/views/report_mailer/usage_report.text.html.erb). How do I do this?
我想使用我app/helpers/annotations_helper.rb在 ReportMailer 视图 ( app/views/report_mailer/usage_report.text.html.erb) 中定义的方法。我该怎么做呢?
Based on this guideit seems like the add_template_helper(helper_module)method might do what I want, but I can't figure out how to use it.
根据本指南,该add_template_helper(helper_module)方法似乎可以满足我的要求,但我不知道如何使用它。
(BTW, is there a reason you get access to a different set of helpers in mailer views? This is pretty annoying.)
(顺便说一句,您是否有理由在邮件程序视图中访问一组不同的帮助程序?这很烦人。)
回答by Mark Connell
In the mailer class that you are using to manage your emails:
在用于管理电子邮件的邮件程序类中:
class ReportMailer < ActionMailer::Base
add_template_helper(AnnotationsHelper)
...
end
回答by Duke
In Rails 3, just use the helper method at the top of your ActionMailer class:
在 Rails 3 中,只需使用 ActionMailer 类顶部的 helper 方法:
helper :mail # loads app/helpers/mail_helper.rb & includes MailHelper
I just passed in a block, since I only need it in the one Mailer:
我刚刚传入了一个块,因为我只需要在一个 Mailer 中使用它:
helper do
def host_url_for(url_path)
root_url.chop + url_path
end
end
(be sure to set config.action_mailer.default_url_options.)
(一定要设置 config.action_mailer.default_url_options。)
(and if you use url_for, be sure to pass in :only_path => false)
(如果你使用 url_for,一定要传入 :only_path => false)
回答by Edison Machado
For all mailers in Rails 3 (setting "application" helper):
对于 Rails 3 中的所有邮件程序(设置“应用程序”助手):
# config/application.rb:
...
config.to_prepare do
ActionMailer::Base.helper "application"
end
回答by Matt
For Ruby on Rails 4, I had to do 2 things:
对于 Ruby on Rails 4,我必须做两件事:
(1) As Dukealready said, if the helper you want to add is UsersHelperfor example, then add
(1) 正如Duke已经说过的,如果您要添加的助手是UsersHelper例如,则添加
helper :users
to the derived ActionMailerclass (e.g. app/mailers/user_mailer.rb)
到派生ActionMailer类(例如app/mailers/user_mailer.rb)
(2) After that, I got a new error:
(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)
To fix this, add the line
要解决此问题,请添加行
config.action_mailer.default_url_options = { :host => 'localhost' }
to each of the config/environments/*.rbfiles. For config/environments/production.rb, replace localhostwith a more appropriate host for the production helper-generated urls.
到每个config/environments/*.rb文件。对于config/environments/production.rb,将localhost生产助手生成的 url替换为更合适的主机。
Q: For #2, why does the mail view need this information, and the regular views do not?
问:对于#2,为什么邮件视图需要这些信息,而常规视图不需要?
A: Because the regular views don't need to know the host, since all generated links are served from the host they link to. Links that show up in emails are not served from the same host (unless you are linking to hotmail.comor gmail.com, etc.)
A:因为常规视图不需要知道host,因为所有生成的链接都是从它们链接到的主机提供的。电子邮件中显示的链接并非来自同一主机(除非您链接到hotmail.com或gmail.com等)
回答by user1136228
You can just add in your mailer
你可以在你的邮件中添加
helper :application
or whatever helper you need
或任何你需要的帮手
回答by MSC
(This is an old question but Rails has evolved so I'm sharing what works for me in Rails 5.2.)
(这是一个老问题,但 Rails 已经发展,所以我将分享 Rails 5.2 中对我有用的内容。)
Typically you might want to use a custom view helper in rendering the subject line of an email as well as the HTML. In the case where the view helper is in app/helpers/application_helper.rbas follows:
通常,您可能希望使用自定义视图助手来呈现电子邮件的主题行以及 HTML。在视图助手位于app/helpers/application_helper.rb 的情况下,如下所示:
module ApplicationHelper
def mydate(time, timezone)
time.in_time_zone(timezone).strftime("%A %-d %B %Y")
end
end
I can create a dynamic email subject line and template which both use the helper but I need to tell Rails to use the ApplicationHelper explicitly in apps/mailer/user_mailer.rbin two different ways, as you can see in the second and third lines here:
我可以创建一个动态电子邮件主题行和模板,它们都使用 helper,但我需要告诉 Rails 以两种不同的方式在apps/mailer/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: '[email protected]',
subject: "Thanks for coming on #{mydate(@party.created_at, @party.timezone)}")
end
end
I should mention that these two lines work just as well so choose one or the other:
我应该提到这两条线也能正常工作,因此请选择其中之一:
helper :application
add_template_helper(ApplicationHelper)
FWIW, the email template at app/views/user_mailer/party_thanks.html.erblooks like this:
FWIW,位于app/views/user_mailer/party_thanks.html.erb的电子邮件模板如下所示:
<p>
Thanks for coming on <%= mydate(@party.created_at, @party.timezone) %>
</p>
And the app/controller/party_controller.rbcontroller looks like this
而应用程序/控制器/ party_controller.rb控制器看起来像这样
class PartyController < ApplicationController
...
def create
...
UserMailer.with(party: @party).party_thanks.deliver_later
...
end
end
I have to agree with OP (@Tom Lehman) and @gabeodess that this all feels quite convoluted given https://guides.rubyonrails.org/action_mailer_basics.html#using-action-mailer-helpersso perhaps I am missing something...
我必须同意 OP (@Tom Lehman) 和 @gabeodess 的观点,鉴于https://guides.rubyonrails.org/action_mailer_basics.html#using-action-mailer-helpers,这一切都让人感觉很复杂,所以也许我错过了一些东西.. .
回答by Yoshi
in my case for Rails4, i do like this:
在我的 Rails4 案例中,我喜欢这样:
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
add_template_helper ApplicationHelper
...
end
and
和
# app/mailers/user_mailer.rb
class AccountMailer < ApplicationMailer
def some_method(x, y)
end
end
so that you do not have to specify add_template_helpereverywhere.
这样您就不必add_template_helper到处指定。

