Jay
Jay

Reputation: 6244

I am getting a error when calling a mailer_helper method for a mailer method in a Rails 3.2.13 app

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" ? "[email protected]" : "[email protected]"
  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.

Upvotes: 0

Views: 422

Answers (2)

MrYoshiji
MrYoshiji

Reputation: 54882

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.

Upvotes: 1

Rails Guy
Rails Guy

Reputation: 3866

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

Upvotes: 1

Related Questions