Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of the action in an after_action filter for ActionMailer

In the class below, how do I get the current action name (i.e. email_confirmation, password_reset) in side the after_action callback add_mandril_headers?

class UserMailer < ActionMailer::Base

  after_action :add_mandril_headers

  def email_confirmation(user)
    mail(..)
  end

  def password_reset(user)
    mail(..)
  end

private

  # how to get the action name? 
  def add_mandrill_headers
    headers['X-MC-Tags'] = [mailer_name, action_name].join('_');
  end
end
like image 809
Harish Shetty Avatar asked Nov 08 '14 22:11

Harish Shetty


People also ask

What is action mailer?

1 Introduction. Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.

What generates information on the mailing run if available?

rb, production. rb, etc...) Generates information on the mailing run if available.

What is a filter in ruby on Rails?

Rails filters are methods that run before or after a controller's action method is executed. They are helpful when you want to ensure that a given block of code runs with whatever action method is called.


1 Answers

Thanks @HarishShetty!

As you mentioned, the action_name is good for all Controllers, as it is inherited from ApplicationController.

For example, I was using public_activity and wanted some simplification in my controllers:

class SiteDetailsController < ApplicationController

  after_action :track_activity, only: [:create, :update, :destroy]

  # ...

  private

  def track_activity
    @site_detail.create_activity action_name, owner: current_user
  end
end
like image 150
Jon Kern Avatar answered Sep 19 '22 12:09

Jon Kern