我正在尝试将pdf文件(使用Rails PDFKit gem生成)保存到回形针附件中。保存生成的PDF文件后,MySQL表条目显示以下是回形针附件:
1 | pdf | !ruby / object:文件{} | NULL | NULL | NULL |
与预期值相反,就像这种格式:
resume.pdf | application / 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"
感谢。
答案 0 :(得分:1)
问题解决了。我忘了在模型中添加以下内容:
has_attached_file:resume_attachment
此外,我查看了this引用,并通过文件类将文件保存在回形针中
my_model_instance = MyModel.new
file = File.open(file_path)
my_model_instance.attachment = file
file.close
my_model_instance.save!