如何在ActionMailer的after_action过滤器中获取操作的名称

时间:2014-11-08 22:43:20

标签: ruby-on-rails actionmailer

在下面的课程中,如何在email_confirmation回调password_reset旁边获取当前操作名称(例如after_actionadd_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

2 个答案:

答案 0 :(得分:15)

结果action_name返回当前的邮件程序操作名称。我基于ActionController具有类似方法的事实尝试了它。

答案 1 :(得分:1)

谢谢@HarishShetty!

正如您所提到的,action_name适用于所有控制器,因为它继承自ApplicationController

例如,我使用public_activity并希望在我的控制器中进行一些简化:

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