ステージングサーバーから送信されるすべてのメールの件名に「[STAGING]」というフレーズを付けてください。ActionMailerを使用してRails3.2でこれを行うためのエレガントな方法はありますか?
			
			1651 次
		
3 に答える
            19        
        
		
これは、既存の回答に基づいてActionMailerInterceptorを使用して見つけたエレガントなソリューションです。
# config/initializers/change_staging_email_subject.rb
if Rails.env.staging?
  class ChangeStagingEmailSubject
    def self.delivering_email(mail)
      mail.subject = "[STAGING] " + mail.subject
    end
  end
  ActionMailer::Base.register_interceptor(ChangeStagingEmailSubject)
end
    于 2012-12-12T00:31:39.797   に答える
    
    
            5        
        
		
これはRails4.xで機能します
class UserMailer < ActionMailer::Base
  after_action do
    mail.subject.prepend('[Staging] ') if Rails.env.staging?
  end
  (...)
end
    于 2017-08-02T03:22:47.047   に答える
    
    
            0        
        
		
実際には、継承はそれが得るのと同じくらいエレガントです。
class OurNewMailer < ActionMailer::Base
  default :from => 'no-reply@example.com',
          :return_path => 'system@example.com'
  def subjectify subject
    return "[STAGING] #{subject}" if Rails.env.staging?
    subject
  end
end
次に、各メーラーから継承できます。
# modified from the rails guides
class Notifier < OurNewMailer
  def welcome(recipient)
    @account = recipient
    mail(:to => recipient.email_address_with_name, :subject => subjectify("Important Message"))
  end
end
これはあなたが望んでいたほどきれいではないと思いますが、これは少し乾きます。
于 2012-12-11T02:35:43.760   に答える