3

帮助器方法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),但是由于助手是在类而不是模块中定义的,所以我不确定该怎么做

4

1 回答 1

4

我喜欢在变量中传递用户,然后在模板中使用它。它将current_user呼叫传递给控制器​​(我通常从中传递邮件)。

另一种解决方案是将current_user和支持方法放入一个模块中,并将其混合到 ApplicationController 中,并与helper继承自 ActionMailer::Base 的类中的方法一起使用。如果您对此方法感兴趣,请查看文档。

于 2010-05-14T02:54:27.450 回答