0

我正在使用 refile gem 上传文件。文件上传后会通过电子邮件发送给某些收件人。

1) 如何将使用 refile 存储的文档作为附件添加到邮件中?下面它不工作

我在数据库中有以下内容:

commented_id, varchar
commented_filename, varchar
commented_size, varchar
commented_content_type, varchar

附上文件:

@revisions.each do |revision|
  mail.attachments["#{revision.file_filename}"] = File.read(revision.file)
end

错误:没有将 Refile::File 隐式转换为 String

2) 我可以动态更改存储文件的位置吗?

4

1 回答 1

0

通过直接在文件上调用 read,而不是使用 #read 作为 File 类方法,我能够将文件从我的 application_mailer.rb 文件中的对象单独附加到邮件程序。我在 Rails 中使用 ActionMailer 并且 mailer 方法看起来像这样,假设它revision是附加了文件的数据库对象,commented如您的 db 中所述:

def send_mailer(revision)
   @revision = revision

   attachments["filename_here"] = @revision.commented.read
   mail(to: "email@email.com", subject: "Here's your file!")
end

不确定您是否也在使用 ActionMailer,但如果是,这可能会起作用!

于 2016-08-19T18:20:54.677 回答