Rails PaperClip电子邮件附件无法正常工作

时间:2014-01-07 16:29:52

标签: ruby-on-rails paperclip

我正在尝试通过电子邮件发送PaperClip附件。我已经阅读了StackExchange上的几篇帖子,但仍然无法让它工作。对于某些尝试,我得到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>>
>>

1 个答案:

答案 0 :(得分:0)

此代码在CommentAttachment模型之间建立一对一的关系:

has_one :attachment

这与Paperclip无关。

我认为在你的Attachment模型中你有以下几点:

has_attached_file :attach

您需要与:attach模型附带的Attachment进行互动:

@comment.attachment.attach

当您尝试将此附加到电子邮件中时,您应该使用以下内容:

attachments[comment.attachment.attach_file_name] = File.read(comment.attachment.attach.path)