0

Following this post and this post I put together the following.

In app/helpers/mailer_helper.rb:

module MailerHelper
  def sys_addy
    @sys_addy = current_site == "site1" ? "me@site1.com" : "me@site2.com"
  end
end

Attempt 1 placing the helper method call above the method - app/mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base
  helper :mailer
  sys_addy
  default :from => @sys_addy

  def notice_email
    ...
  end
end

Attempt 2 placing the helper method call inside the method - app/mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base
  helper :mailer

  def notice_email
    sys_addy
    ...
    mail( ...:from => @sys_addy )
  end
end

In both cases I got:

NameError (undefined local variable or method `sys_addy'

Thanks for your help.

Community
  • 1
  • 1
Jay
  • 6,206
  • 11
  • 48
  • 82
  • 2
    I think you wanted to use `include MailerHelper` in your UserMailer model – MrYoshiji Aug 15 '13 at 17:36
  • @MrYoshiji, using include MailerHelper is producing the same error. – Jay Aug 15 '13 at 17:44
  • 1
    Check this: http://stackoverflow.com/questions/6795109/make-a-custom-helper-available-to-both-mailer-and-view-in-rails-3-1 – MrYoshiji Aug 15 '13 at 17:45
  • @MrYoshiji, so instead of a mailer_helper file, I should put the method in a controller and then include that controller in the mailer file. Working on it but am getting the same error so far. – Jay Aug 15 '13 at 18:03
  • 1
    no no, just try with both `include MailerHelper` and right after it `helper :mailer` – MrYoshiji Aug 15 '13 at 18:10
  • @MrYoshiji, thanks. You got my a step closer. "current_site" is designated as a helper_method in my application controller which is throwing "NameError (undefined local variable or method `current_site' for # – Jay Aug 15 '13 at 19:59
  • 1
    You can use `Thread.current[:site] = site` to store "dynamic" data accross your App. But it is not really safe, have a look at it if you are interested ;) – MrYoshiji Aug 15 '13 at 20:03
  • @MrYoshiji, thanks for the suggestion. Better that i don't create vulnerability in the app tho. – Jay Aug 15 '13 at 20:07
  • @MrYoshiji, if you put your solution that got me 'a step closer' into an answer, then I'll select your answer as the solution. – Jay Aug 16 '13 at 13:22

2 Answers2

1

I think the problem is you are trying to define it as a symbol. Try calling it like Class like below in your User Mailer:

class UserMailer < ActionMailer::Base
   include MailerHelper
   helper :mailer       

  def notice_email
    sys_addy
    ...
    mail( ...:from => @sys_addy )
  end
end

Update : use both (include & helper) or try with below :

add_template_helper(MailerHelper)

This way, you can access this method in your views as well. Hope, it will help.Thanks

Rails Guy
  • 3,836
  • 1
  • 20
  • 18
  • I tried "helper MailerHelper" instead of "helper :mailer" and it produced "NameError (undefined local variable or method `sys_addy' for #)". Thanks for suggestion. – Jay Aug 15 '13 at 19:46
  • 1
    your answer is a summary of the solution MrYoshiji's gave me in comments that got me a step closer. In fairness to him, if he puts his solution into an answer within the next few days I will select his answer as the solution. If I don't hear from him, I'll select yours. I did upvote each of your responses tho. Thanks for your help. – Jay Aug 16 '13 at 13:27
1

I think you can use the following:

class UserMailer < ActionMailer::Base
  include MailerHelper
  helper :mailer

But the problem with this solution is that the Helper can't know the result of current_site (I guess it is stocked in the User's session?).

A workaround is to use Thread.current in order to store data dynamically (can be updated):

Thread.current[:site] = site

This solution is not that safe, make sure to secure the access to this variable and also never put user's input in the Thread.current.

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117