1

I'm sending out emails to users and I need to call a function I have in my ApplicationController. Seems simple enough, but I cannot for the life of me find any documentation on how to to this. I can access ApplicationHelper functions just fine, but that's not doing me much good for what I need it to do... Anyone got any light they can shed on the subject?

Ralph Haynes
  • 83
  • 10
  • what is the function you're needing to pass and why? Where are you calling the mailer from? Your model or your controller? Some code might help. – Richard Jordan Feb 03 '14 at 00:55
  • The site allows health care providers to create coupons for users to download. When a user signs up and confirms their account, I'm sending a welcome email which I will show a few coupons from health care providers close to them. The function is to find these coupons which is the same function I use throughout the site which takes several parameters (location, max distance, service category, etc.). Mailer is being called from the User Model, so I can't call the function and pass the coupons to the mailer as a parameter as if I was calling it from the controller. – Ralph Haynes Feb 03 '14 at 01:20

2 Answers2

3

So not sure if this will help anybody out since this was a highly project specific case, but here's what I did to work through this:

  1. Move functions required for the mailer to the ApplicationHelper.
  2. Add include ApplicationHelper to the Mailer class and my ApplicationController.
  3. For the logic where I needed if !provider_signed_in? I updated to if defined?(provider_signed_in?) && !provider_signed_in? so that it wouldn't return an undefined method error... The logic that I needed if the provider was signed in was irrelevant for the mailer (since a provider would never get this email), so it didn't matter there even though it does matter everywhere else I'm using it on the site.

Thanks Rahul for helping me think through this.

Ralph Haynes
  • 83
  • 10
0

there is a method called helper which you can use in your mailer to inlude your helper in your mailer..

# mailer_helper.rb

module MailerHelper
  def your_method
    # do whatever you want to do here.
  end
end

# in your mailer file

class WelcomeMailer < ActionMailer::Base
  helper MailerHelper
  .....
end

and that't it, now you can use the methods in the views also.

Rahul Singh
  • 3,417
  • 2
  • 25
  • 32
  • not sure but you can use include also in your mailer, like "include MailerHelper" – Rahul Singh Feb 03 '14 at 06:20
  • Helpers aren't really going to do me much good. The function really needs to stay in ApplicationController because I'm using it all over the site and it relies on other functions such as geolocation and a few other functions also in ApplicationController. Using a helper would mean I would have to duplicate several functions, so it seems incredibly redundant to do so. – Ralph Haynes Feb 03 '14 at 15:24
  • you can define all your ApplicationController methods in helper eg, in ApplicationHelper, and then you can include ApplicationHelper in your ApplicationController so now u can access all you methods in controller as well as mailer too and in any view also. – Rahul Singh Feb 03 '14 at 15:47
  • I tried that at one point and it almost does the trick, but the only problem is that part of the logic has to call **user_signed_in?** (Using Devise for user accounts)... which comes back as an undefined method when used in a helper. – Ralph Haynes Feb 03 '14 at 16:24
  • it is not mandatory that u have to define all your methods in helper...define only those methods in helper which u want to access in controller,mailer and views...and leave all other methods in ApplicationController...are you using user_signed_in? inside your mailer..? – Rahul Singh Feb 03 '14 at 16:32
  • Yeah I'll need that logic to stay in the function for the controller and mailer... I can work around the **user_signed_in?** for the mailer, but it's actually **provider_signed_in?** that I need for this particular instance... Users and providers obviously have different account types. – Ralph Haynes Feb 03 '14 at 16:46
  • then m sorry, may b then u should call this method before delivering your mail somewhere in controller.. – Rahul Singh Feb 03 '14 at 17:01