[英]Rails PaperClip email attachment not working
我正在嘗試通過電子郵件發送PaperClip附件。 我已經閱讀了StackExchange上的幾篇文章,但仍然無法使它正常工作。 對於某些嘗試,我會獲得undefined method'
,而其他嘗試undefined method'
我帶到登錄屏幕。
評論模型:
has_one :attachment
我嘗試過的郵件程序代碼:
attachments[comment.attachment.attach_file_name] = File.read(comment.attachment.to_file.path)
attachments[comment.attachment.attach_file_name] = File.read(comment.attachment.path)
attachments[comment.attachment.attach_file_name] = File.read(attachment_path(comment.attachment))
這是瀏覽器控制台的副本-我嘗試了幾件事:
>> comment.attachment.attach_file_name
=> "2013-09-10_09-32-32.png"
>> comment.attachment.path
!! #<NoMethodError: undefined method `path' for # <Attachment:0x007f8d06903450>>
>> attachment_path(comment.attachment)
=> "/attachments/75"
>> File.read(attachment_path(comment.attachment))
!! #<Errno::ENOENT: No such file or directory - /attachments/75>
>> File.read(attachment_path(comment.attachment).to_file)
!! #<NoMethodError: undefined method `to_file' for "/attachments/75":String>
>> File.read(comment.attachment.path)
!! #<NoMethodError: undefined method `path' for # <Attachment:0x007f8d06903450>>
>> File.read(comment.attachment.to_file.path)
!! #<NoMethodError: undefined method `to_file' for # <Attachment:0x007f8d06903450>>
>>
因此,此代碼:
attachment_path(comment.attachment)
返回此:
"/attachments/75"
UDPATE
我嘗試了這個:
attachments[comment.attachment.attach_file_name] = File.read(comment.attachment.attach_url)
有undefined method attach_url
因此,請在控制台中嘗試以下操作:
>> comment.attachment.attach_url
!! #<NoMethodError: undefined method `attach_url' for # <Attachment:0x007f8d1c9b3cb8>>
>> attachment_path(comment.attachment)
=> "/attachments/76"
>> attachment_path(comment.attachment).attach_url
!! #<NoMethodError: undefined method `attach_url' for "/attachments/76":String>
>> attach_url(comment.attachment)
!! #<NoMethodError: undefined method `attach_url' for #<CommentMailer:0x007f8d145664d8>>
>> attach_url_path(comment.attachment)
!! #<NoMethodError: undefined method `attach_url_path' for # <CommentMailer:0x007f8d145664d8>>
>> attachment(comment.attachment)
!! #<NoMethodError: undefined method `attachment' for # <CommentMailer:0x007f8d145664d8>>
>>
這段代碼在Comment
和Attachment
模型之間建立了一對一的關系:
has_one :attachment
這與回形針無關。
我假設在您的Attachment
模型中,您可以按照以下方式進行操作:
has_attached_file :attach
您需要與附加到Attachment
模型的:attach
交互:
@comment.attachment.attach
當您嘗試將其附加到您的電子郵件時,您應該使用如下格式:
attachments[comment.attachment.attach_file_name] = File.read(comment.attachment.attach.path)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.