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

≡放荡痞女 提交于 2019-12-08 15:38:13

问题


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

回答1:


Turns out action_name returns the current mailer action name. I tried it based on the fact that ActionController has a similar method.




回答2:


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


来源:https://stackoverflow.com/questions/26822926/how-to-get-the-name-of-the-action-in-an-after-action-filter-for-actionmailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!