【问题标题】:Rails 4.1 -> 4.2 has caused an after_create callback that sends an e-mail to stop workingRails 4.1 -> 4.2 导致了一个 after_create 回调,该回调发送一封电子邮件停止工作
【发布时间】:2015-06-03 16:42:30
【问题描述】:

在我的 Note 模型 (note.rb) 中,下面的 sn-p 由 Active Record 回调触发:

def creation_email
  case self.notable_type
  when 'Application'
    NoteMailer.run('application', self)
  when 'Appointment'
    NoteMailer.run('appointment', self)
  end

  self.inbounded = 1
  self.save
end

NoteMailer 中,我有另一个switch 语句来发送基于多态notable_type 的不同电子邮件模板。这是sn-p:

  def run(type, note)
    @note = note

    case type
    when 'application'
      app = Application.find(@note.notable_id)

      app.users.each do |user|
        if user.id != @note.user.id && user.email
          mail(to: user.email,
               from: @note.user.name + " <application." + app.id.to_s + "." + user.id.to_s + "@mail.mysecretdomain.com>",
               subject: "my secret subject",
               template_name: "application.html.erb").deliver_now
        end   
      end
    when 'appointment'
      a = Appointment.find(@note.notable_id)

      a.users.each do |user|
        if user.id != @note.user.id && user.email
          mail(to: user.email,
               from: note.user.name + " <appointment." + a.id.to_s + "." + user.id.to_s + "@mail.mysecretdomain.com>",
               subject: "my secret subject",
               template_name: "appointment.html.erb").deliver_now
        end
      end
    end
  end

自从从 Rails 4.1 升级到 Rails 4.2 后,电子邮件不再像以前那样发送出去了。

什么可能导致这个问题?我确实将mail(..).deliver 更改为mail(..).deliver_now

我尝试在顶部的 run 方法中添加puts "test",但它并没有将字符串放入控制台。根本不会抛出任何错误

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    问题是 run 永远不会被调用。您需要在适当的地方拨打deliver_now

    试试这个:

    def creation_email
      case self.notable_type
      when 'Application'
        NoteMailer.run('application', self).deliver_now
      when 'Appointment'
        NoteMailer.run('appointment', self).deliver_now
      end
    
      self.inbounded = 1
      self.save
    end
    

    而不是在您的邮件程序中调用deliver_now

    http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-1-to-rails-4-2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2012-05-14
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多