[英]Rails PaperClip email attachment not working
I'm trying to email a PaperClip attachment. 我正在尝试通过电子邮件发送PaperClip附件。 I've read several posts on StackExchange, but still can't get it to work.
我已经阅读了StackExchange上的几篇文章,但仍然无法使它正常工作。 For some tries I get
undefined method'
, others take me to the login screen. 对于某些尝试,我会获得
undefined method'
,而其他尝试undefined method'
我带到登录屏幕。
Comment Model: 评论模型:
has_one :attachment
Mailer code I've tried: 我尝试过的邮件程序代码:
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))
This is a copy of the browser console - I tried several things: 这是浏览器控制台的副本-我尝试了几件事:
>> 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>>
>>
So, this code: 因此,此代码:
attachment_path(comment.attachment)
Returns this: 返回此:
"/attachments/75"
UDPATE UDPATE
I tried this: 我尝试了这个:
attachments[comment.attachment.attach_file_name] = File.read(comment.attachment.attach_url)
Got undefined method attach_url
有
undefined method attach_url
So, tried these in the console: 因此,请在控制台中尝试以下操作:
>> 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>>
>>
This code sets up a one-to-one relationship between the Comment
and the Attachment
model: 这段代码在
Comment
和Attachment
模型之间建立了一对一的关系:
has_one :attachment
This has nothing to do with Paperclip. 这与回形针无关。
I assume that within your Attachment
model you have something along the lines of the following: 我假设在您的
Attachment
模型中,您可以按照以下方式进行操作:
has_attached_file :attach
You need to interact with the :attach
attached to the Attachment
model: 您需要与附加到
Attachment
模型的:attach
交互:
@comment.attachment.attach
When you're trying to attach this to your email you should be using something like this: 当您尝试将其附加到您的电子邮件时,您应该使用如下格式:
attachments[comment.attachment.attach_file_name] = File.read(comment.attachment.attach.path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.