Access helpers from mailer?

前端 未结 11 1824
夕颜
夕颜 2020-12-05 17:25

I have trying to access helper methods from a rails 3 mailer in order to access the current user for the session.

I put the helper :application in my mailer class,

11条回答
  •  鱼传尺愫
    2020-12-05 17:32

    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:

    class CommentMailer < ActionMailer::Base
      default :from => "Andre Fournier "
    
      def blog_comment(user)
        @recipients                   = user.email
        @from                         = "andre@gfournier.com"
        @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

    Separately, you can make a helper called

    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.

提交回复
热议问题