我試圖通過電子郵件發送PaperClip附件。我已經閱讀了StackExchange上的幾篇文章,但仍然無法讓它起作用。對於某些嘗試我得到undefined method'
,其他人帶我到登錄屏幕。Rails PaperClip電子郵件附件不起作用
評論模型:
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.attach.url' – Jon
'comment.attachment.attach.path'應該做的竅門(url返回相對路徑,並且.path返回文件存儲在您的位置的完整路徑服務器) – MrYoshiji