【发布时间】:2014-02-16 07:23:50
【问题描述】:
我有一个 rake 任务,它获取 XML 文档并将它们保存到我的应用程序中的模型中。该任务一直有效,直到我将 content_type 验证添加到模型中
任务现在给出错误:
Datafile content type is invalid, Datafile is invalid
如何将 tmp 文件的 content_type 设置为 text/xml 以便验证通过?
任务代码如下:
task :fetch_documents => :environment do
Net::FTP.open("www.example.com", "xxxxx", "xxxxx") do |ftp|
ftp.nlst.each do |file_name|
tmp = Tempfile.new(['foo', '.xml' ])
ftp.gettextfile(file_name, tmp.path)
# save it
document = Document.new
document.file_name = File.basename(file_name)
document.datafile = tmp
document.save!
tmp.close
end
end
end
【问题讨论】:
-
这可能会有所帮助 - stackoverflow.com/questions/11621863/…
-
@devanand 谢谢,但不只是 s3,我只是在本地运行任务。
-
@devanand 实际上,这确实有帮助:) - 它导致使用 MIME::Types.type_for("filename.xml").first.content_type 来获取内容类型,即 application/xml - 将其添加到模型验证中让我通过了。谢谢!