【问题标题】:Using a helper method in a mailer that is defined in a controller在控制器中定义的邮件程序中使用辅助方法
【发布时间】:2010-05-14 02:36:06
【问题描述】:

ApplicationController 中定义了帮助器方法current_user 并作为帮助器提供,如下所示:

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

【问题讨论】:

    标签: ruby-on-rails ruby helper


    【解决方案1】:

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

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

    【讨论】:

    • 我在邮件方法参数中传递了控制器方法的返回值。 UserMailer.invitation(@invitation, current_organization).deliver 其中 current_organization 定义在 ApplicationController
    猜你喜欢
    • 2012-11-04
    • 1970-01-01
    • 2011-02-05
    • 2017-05-01
    • 2019-12-30
    • 1970-01-01
    • 2021-05-20
    • 2011-01-24
    • 2023-04-05
    相关资源
    最近更新 更多