【问题标题】:Not able to save PDF Kit generated file to a Paperclip attachment in Rails 3.2.15无法将 PDF Kit 生成的文件保存到 Rails 3.2.15 中的回形针附件
【发布时间】:2023-04-11 08:22:01
【问题描述】:

我正在尝试将 pdf 文件(使用 Rails PDFKit gem 生成)保存到回形针附件中。保存生成的 PDF 文件后,MySQL 表条目显示回形针附件的以下内容:

1 | PDF格式 | !ruby/object:文件 {} |空 |空 |空|

相对于预期值是这样的格式:

简历.pdf |申请/pdf | 38375 | 2014-01-30 18:24:34

谁能告诉我这里出了什么问题?

控制器:

html = render_to_string('resume.html.erb',layout: false)
kit = PDFKit.new(html, :page_size => 'Letter')

file_name = "resume"

path = "#{Rails.root}/app/assets/PDF/" + file_name.to_s + ".pdf"
**file** = kit.to_file(path)

document = Document.new(:resume_type=>"pdf")
document.resume_attachment_file_name = **file**
document.save! 

schema.rb

create_table "documents", :force => true do |t|
t.string   "resume_type"
t.string   "resume_attachment_file_name"
t.string   "resume_attachment_content_type"
t.integer  "resume_attachment_file_size"
t.datetime "resume_attachment_updated_at"

谢谢。

【问题讨论】:

    标签: mysql ruby-on-rails-3.2 amazon-s3 paperclip pdfkit


    【解决方案1】:

    问题解决了。我忘了在模型中添加以下内容:

    has_attached_file :resume_attachment

    另外,我查看了this参考,通过File类将文件保存在回形针中

     my_model_instance = MyModel.new
     file = File.open(file_path)
     my_model_instance.attachment = file
     file.close
     my_model_instance.save!
    

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 1970-01-01
      • 2011-06-29
      • 2018-04-29
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多