29

I have a method that takes an array of strings and joins them so they do something like this:

>> my_arr
=> ["A", "B", "C"]
>> and_join(my_arr)
=> "A, B, and C"

Which I'd like my mailer to have access to so I can output some information into an email. I can't seem to find a good place to put it and putting it in the application_helper.rb file and it doesn't find it there. Where should it go?

3
  • 3
    Worth looking at the to_sentence extension method for arrays that rails provides.
    – Shadwell
    Sep 10, 2010 at 8:32
  • @Shadwell, post that as an answer and I'll +1 it. That is exactly what I was looking for.
    – aarona
    Sep 10, 2010 at 18:13
  • similar one - amolnpujari.wordpress.com/2013/12/27/… Dec 27, 2013 at 11:32

6 Answers 6

74

Use the helper method in your mailer to define the helper class to use

# mailer_helper.rb
module MailerHelper
  def and_join(arr)
    # whatever …
  end
end

# my_mailer.rb
class MyMailer < ActionMailer::Base
  helper MailerHelper
  …
end

then you can use the methods in views as well.

1
  • 6
    Great. Add include MailerHelper to MyMailer < ActionMailer::Base as well to use the helper methods in My Mailer (like a subject formatter) in addition to any downstream views
    – Chris Beck
    Jan 5, 2014 at 15:39
10

It would be worth looking at the to_sentence extension method for arrays that rails provides.

0
5

An already answered question, but I didn't get where/what-file to modify from the other SO responses. Here is how I did it:

At the bottom of app/config/initializers/devise.rb you can add this:

Devise::Mailer.class_eval do
  helper :application # includes "ApplicationHelper"
end

This example will include the methods in /app/helpers/application_helper.rb. You could include another helper-file instead - if, for example, the method is only for the mailer or used for one other controller only. The def I needed is used all over, so I put in in that file so all views could access it.

5

In my case, for Rails 5.1, I had to use both include and helper methods, like this:

include ApplicationHelper
helper :application

And then just proceed to use the method normally.

class MyMailer < ActionMailer::Base
  include ApplicationHelper
  helper :application

  def my_mailer_method
    my_helper_method_declared_in_application_helper
    ..
  end
end
5

If you have some one off methods you want to use in the view, you can use helper_method directly in your mailer.

class MyMailer < ApplicationMailer
  def mailer
    mail to: '', subject: ''
  end

  private

  helper_method def something_to_use_in_the_view
  end
end

something_to_use_in_the_view will be available in your view.

4

+1, worked fine, just a little correction:

You have to use module instead of class in the helper file:

# mailer_helper.rb
module MailerHelper
  def and_join(arr)
    # whatever …
  end
end

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.