Ruby on rails Rails:HTML电子邮件包含对由附件处理的S3托管图像的引用,导致无对象错误

Ruby on rails Rails:HTML电子邮件包含对由附件处理的S3托管图像的引用,导致无对象错误,ruby-on-rails,Ruby On Rails,在提交一个条目之后,我试图发送一封HTML电子邮件,其中包含一张托管在AmazonS3上的照片(由attachment_fu处理) 当我尝试引用ActionMailer中某个条目的照片时,我一直收到一个nil对象错误。这是相关的代码 class Entry < ActiveRecord::Base include AASM aasm_initial_state :new aasm_state :new aasm_state :pending aasm_state

在提交一个条目之后,我试图发送一封HTML电子邮件,其中包含一张托管在AmazonS3上的照片(由attachment_fu处理)

当我尝试引用ActionMailer中某个条目的照片时,我一直收到一个nil对象错误。这是相关的代码

class Entry < ActiveRecord::Base

  include AASM

  aasm_initial_state :new

  aasm_state :new
  aasm_state :pending
  aasm_state :rejected, :enter => :reject_entry
  aasm_state :approved, :enter => :approve_entry
  aasm_state :archived
  aasm_state :deleted

  has_one :photo, :dependent => :destroy

  ...

  aasm_event :pending do
    transitions :to => :pending, :from => [:new], :guard => 'process_new_entry'
  end

  ...

  def process_new_entry
    self.make_approval_code
    EntryMailer.deliver_pending_entry_notification(self)
  end
  ...
end
我可以成功获取有关该条目的所有其他信息(包括其他关联),但尝试引用entry.photo失败


有什么想法吗?

它看起来像是
条目。photo
正在返回
nil
,如果特定的
条目
没有与之关联的
照片
,就会出现这种情况

在尝试调用
authenticated\u s3\u url(:thumb)
之前,您需要检查entry.photo

@body[:photo_url] = entry.photo.authenticated_s3_url(:thumb) if entry.photo

或者一些变化可能会起作用(ymmv,我没有测试)

当邮件程序启动时,我的条目对象已过时。在触发邮件程序之前重新加载对象解决了这个问题

class Entry < ActiveRecord::Base
  ...

  def process_new_entry
    self.make_approval_code
    self.reload
    EntryMailer.deliver_pending_entry_notification(self)
  end
  ...
end
类条目
保存条目需要照片,因此所有条目都必须有照片。在我的应用程序的其余部分中使用entry.photo可以正常工作,但在ActionMailer中失败。
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.authenticated_s3_url
@body[:photo_url] = entry.photo.authenticated_s3_url(:thumb) if entry.photo
class Entry < ActiveRecord::Base
  ...

  def process_new_entry
    self.make_approval_code
    self.reload
    EntryMailer.deliver_pending_entry_notification(self)
  end
  ...
end