问题
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