Rails条件邮件程序

时间:2014-08-21 12:23:59

标签: ruby-on-rails ruby-on-rails-3 conditional actionmailer conditional-statements

我有一个跟踪通话的Rails应用。每次生成新的呼叫时,我都会通过medic_email字段向分配给该单位的医务人员(这是一个协会)的工作人员发送一封电子邮件,并将新呼叫发送到address@domain.com。

目前我的工作正常,但我想在邮件中添加一个条件,以某种方式根据呼叫区域(呼叫和区域之间的关联)更改cc:地址。因此,休斯顿地区的所有电话都会被分配给拨打电话的医务人员,并且会发送电子邮件至houston@domain.com,并且达拉斯地区的所有电话都会被发送给分配给电话会议的医务人员,并且会转到dallas @ domain .COM

根据我编写的当前代码,在邮件程序中设置条件的正确方法是什么?以下是一些可能有助于更好地解释问题的例外情况。如果有任何不清楚的地方,请告诉我,我会修改我的问题。

call_mailer-摘录

  def new_call(medic, call)
    @call = call
    @medic = medic

    mail to: [@medic.medic_email], :cc => "alerts@domain.com", subject: "New Call: #{@call.incident_number}"
  end

call.rb-摘录

has_many :call_units
has_many :units, through: :call_units
belongs_to :region

  def send_mail(mail_type)
    units.each do |unit|
      CallMailer.send(mail_type, unit.incharge,  self).deliver
      CallMailer.send(mail_type, unit.attendant, self).deliver
    end
  end

region.rb-摘录

attr_accessible :area
  has_many :calls

calls_controller.rb

  def create
    parse_times!
    @call = Call.new(params[:call])
    @call.dispatched_by = current_user.username

     if @call.save
      if @call.unit_ids.present?
        @call.send_mail(:new_call)
        send_new_sms
        redirect_to calls_path, notice: "Call #{@call.incident_number} was successfully created.".html_safe
      else
        redirect_to calls_path, notice: "Call #{@call.incident_number} was successfully created.".html_safe
      end
      else
        render :new
     end
  end

unit.rb

has_many :call_units
has_many :calls, through: :call_units
belongs_to :attendant, :foreign_key => :attendant_id, :class_name => 'Medic'
belongs_to :incharge, :foreign_key => :incharge_id, :class_name => 'Medic'

medic.rb-摘录

has_many :units
has_many :units_attendant_name, :foreign_key => :attendant_id, :class_name => 'Unit'
has_many :units_incharge_name, :foreign_key => :incharge_id, :class_name => 'Unit'

1 个答案:

答案 0 :(得分:2)

最简单的方法是添加一个" copy_mail"列表到Region表。

rake db:migrate AddCopyMailToRegion copy_mail:string

使用正确的电子邮件地址填充它,然后在邮件程序中引用它...

mail to: [@medic.medic_email], :cc => @call.region.copy_mail, subject: "New Call: #{@call.incident_number}"