Ruby-on-rails 从邮件程序访问助手?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4937208/
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
Access helpers from mailer?
提问by Sean O'Hara
I have trying to access helper methods from a rails 3 mailer in order to access the current user for the session.
我试图从 rails 3 邮件程序访问帮助程序方法,以便访问会话的当前用户。
I put the helper :application in my mailer class, which seems to work, except the methods defined therein are not available to my mailer (i get undefined errors). Does anyone know how this is supposed to work?
我将 helper :application 放在我的邮件程序类中,这似乎可以工作,但其中定义的方法对我的邮件程序不可用(我收到未定义的错误)。有谁知道这应该如何工作?
Here's my class:
这是我的课:
class CommentMailer < ActionMailer::Base
default :from => "Andre Fournier <[email protected]>"
helper :application
end
Thanks, Sean
谢谢,肖恩
回答by William Denniss
To enable you to access application helpers from the ActionMailer views, try adding this:
要使您能够从 ActionMailer 视图访问应用程序助手,请尝试添加以下内容:
add_template_helper(ApplicationHelper)
To your ActionMailer (just under your default :fromline).
到您的 ActionMailer(就在您的default :from行下)。
回答by Joshua Pinter
Use helper ApplicationHelper
用 helper ApplicationHelper
class NotificationsMailer < ActionMailer::Base
default from: "Community Point <[email protected]>"
helper ApplicationHelper
helper NotificationMailerHelper
# ...other code...
NOTE: These helper methods are only available to the Views. They are not available in the mailer class (NotificationMailerin my example).
注意:这些辅助方法仅适用于 Views。它们在邮件程序类中不可用(NotificationMailer在我的示例中)。
If you need them in the actual mailer class, use include ApplicationHelper, like so:
如果您在实际的邮件程序类中需要它们,请使用include ApplicationHelper,如下所示:
class NotificationMailer < ActionMailer::Base
include ApplicationHelper
# ... the rest of your mailer class.
end
From this other SO question.
回答by Questor
This is a very old question, but I don't see the full answer, so I will try as I didn't find another resource.
这是一个非常古老的问题,但我没有看到完整的答案,所以我会尝试,因为我没有找到其他资源。
It depends on what you are doing with the methods that have been defined in the helper module. If they are class methods, and everything that's not called on a specific instance seem to be a class methods for 3.2.13, you need to use
这取决于您使用 helper 模块中定义的方法做什么。如果它们是类方法,并且在特定实例上未调用的所有内容似乎都是 3.2.13 的类方法,则需要使用
extend ApplicationHelper
if an instance methods
如果一个实例方法
include ApplicationHelper
and if you want to use them in a mailer view
如果您想在邮件程序视图中使用它们
helper ApplicationHelper
回答by dangerousdave
You could try mixing in the required helper module:
您可以尝试混合所需的辅助模块:
class CommentMailer < ActionMailer::Base
include ApplicationHelper
end
回答by B Seven
Josh Pinter's answer is correct, but I discovered that it is not necessary.
Josh Pinter 的回答是正确的,但我发现没有必要。
What isnecessary is to name the helper correctly.
什么是必要的正确命名的帮手。
NotificationMailerHelperis correct. NotificationMailersHelper(note the s) is not correct.
NotificationMailerHelper是正确的。NotificationMailersHelper(注意 s)不正确。
The class and filename of the helper must match and be correctly spelled.
帮助程序的类和文件名必须匹配并且拼写正确。
Rails 3.2.2
导轨 3.2.2
回答by wetjosh
include ActionView::Helpers::TextHelperworked for me in the Mailer controller (.rb file). This allowed me to use the pluralize helper in a Mailer controller action (helpers worked fine from the get go in Mailer views). None of the other answers worked, at least not on Rails 4.2
include ActionView::Helpers::TextHelper在 Mailer 控制器(.rb 文件)中对我来说有效。这允许我在 Mailer 控制器操作中使用复数帮助器(从 Mailer 视图中的开始,帮助器工作正常)。其他答案均无效,至少在 Rails 4.2 上无效
回答by stcorbett
The single method version of promoting a method to being a helper that is available in ApplicationController also works in ActionMailer:
ApplicationController 中提供的将方法提升为助手的单一方法版本也适用于 ActionMailer:
class ApplicationMailer < ActionMailer::Base
helper_method :marketing_host
def marketing_host
"marketing.yoursite.com"
end
end
from there you can call marketing_hostfrom any of your mailer views
从那里您可以marketing_host从任何邮件程序视图中调用
回答by PythonDev
If you want to call helper method from ActionMailer you need to include helper (module) in Mailer file as, if Helper module name is “UserHelper”, then need to write following in Mailer file
如果你想从 ActionMailer 调用 helper 方法,你需要在 Mailer 文件中包含 helper(模块),如果 Helper 模块名称是“UserHelper”,则需要在 Mailer 文件中写入以下内容
class CommentMailer < ActionMailer::Base
default :from => "Andre Fournier <[email protected]>"
add_template_helper(UserHelper)
end
Or
或者
class CommentMailer < ActionMailer::Base
default :from => "Andre Fournier <[email protected]>"
include UserHelper
end
Hope this is helpful.
希望这是有帮助的。
回答by fengolly
I'm not sure exactly what you are doing here, but when I want to access current_user from a mailer, I make a mailer method that I pass the user to as an argument:
我不确定你在这里做什么,但是当我想从邮件程序访问 current_user 时,我创建了一个邮件程序方法,我将用户作为参数传递给它:
class CommentMailer < ActionMailer::Base
default :from => "Andre Fournier <[email protected]>"
def blog_comment(user)
@recipients = user.email
@from = "[email protected]"
@sent_on = Time.now
@timestamp = Time.now
@user = user
end
end
With the above, @user, as well as all the other instance variables, are accessible from inside the mailer views ./views/comment_mailer/blog_comment.html.erb and ./views/comment_mailer/blog_comment.text.erb
有了上面的内容,@user 以及所有其他实例变量都可以从邮件程序视图中访问 ./views/comment_mailer/blog_comment.html.erb 和 ./views/comment_mailer/blog_comment.text.erb
Separately, you can make a helper called
另外,您可以创建一个名为的助手
comment_mailer_helper.rb
comment_mailer_helper.rb
and put into that helper any methods that you want to be available to your mailer's views. This seems to me more like what you might want, regarding helpers, because helpers are designed to help views, whereas a mailer is analogous to a controller.
并将您希望对邮件程序的视图可用的任何方法放入该助手中。这在我看来更像是您可能想要的,关于助手,因为助手旨在帮助视图,而邮件程序类似于控制器。
回答by Asarluhi
None of the *_pathhelpers are accessible by default inside of an email. It is necessary instead to use the *_urlform of the wanted helper. So, for instance, instead of using user_path(@user)it is necessary to use user_url(@user).
See at Action Mailer basics.
*_path默认情况下,电子邮件中没有任何帮助程序可访问。有必要使用*_url通缉助手的形式。因此,例如,不是使用user_path(@user)而是使用user_url(@user). 请参阅Action Mailer 基础知识。

