开发者

Using a helper method in a mailer that is defined in a controller

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_u开发者_如何学Goser = 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


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 the helper method in your class that inherits from ActionMailer::Base. Take a look at the docs if you're interested in this method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜