在控制器中定义的邮件程序中使用辅助方法
辅助方法 current_user
已定义并可用作 ApplicationController
中的辅助方法,如下所示:
class ApplicationController < ActionController::Base
helper_method :current_user
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
end
我的问题是如何在中使用 current_user
辅助方法邮件程序模板(显然它总是返回nil
,但我正在尝试渲染依赖于它的部分)。
通常,当我想在邮件程序中使用助手时,我会执行类似 add_template_helper(SongsHelper)
的操作,但由于助手是在类而不是模块中定义的,我不确定该怎么做
The helper method current_user
is defined and made available as a helper in ApplicationController
like this:
class ApplicationController < ActionController::Base
helper_method :current_user
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
end
My question is how I can use the current_user
helper method in a mailer template (obviously it will always return nil
, but I'm trying to render a partial that depends on it).
Normally when I want to use helpers in a mailer, I do something like add_template_helper(SongsHelper)
, but since the helper is defined in a class instead of a module I'm not sure what to do
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我喜欢将用户传递到变量中,然后在模板中使用它。它将
current_user
调用传递给控制器(我通常从那里传递邮件)。另一种解决方案是将
current_user
和支持方法放入模块中,并将其混合到 ApplicationController 中,并与继承自 ActionMailer::Base 的类中的helper
方法一起使用。如果您对此方法感兴趣,请查看文档。I like to pass the user in a variable, then just use that in the template. It passes off the
current_user
call to the controller (where I usually deliver mail from).Another solution is to put
current_user
and supporting methods into a module and mix it into both ApplicationController and also use with thehelper
method in your class that inherits from ActionMailer::Base. Take a look at the docs if you're interested in this method.