2014-01-07 74 views
1

我試圖通過電子郵件發送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>> 
>> 
+0

這應該是'comment.attachment.attach.url' – Jon

+0

'comment.attachment.attach.path'應該做的竅門(url返回相對路徑,並且.path返回文件存儲在您的位置的完整路徑服務器) – MrYoshiji

回答

0

此代碼設置了CommentAttachment模型之間的一個一對一的關係:

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) 
+0

感謝您的幫助! 'has_attached_file:attach'嘗試了File.read(comment.attachment.attach.to_file.path)。我爲#' – Reddirt

+0

得到以下'未定義的方法'to_file'我更新了我的答案以使用'comment.attachment.attach.path' ...你可以試試嗎? – Jon

+0

這工作:'attachments [comment.attachment.attach_file_name] = File.read(comment.attachment.attach.path)' – Reddirt