我有以下内容:
attachments.each do |a|
Rails.logger.info a.filename
tempfile = Tempfile.new("#{a.filename}", "#{Rails.root.to_s}/tmp/")
Rails.logger.info tempfile.path
end
附件来自回形针。
这是输出:
billgates.jpg
/Users/bhellman/Sites/cline/tmp/billgates.jpg20101204-17402-of0u9o-0
为什么文件名末尾附加了 20101204-17402-of0u9o-0
?这就是用回形针之类的东西打破一切。以前有人见过这个吗?我一生都不知道在做什么?
谢谢
更新 回形针:Paperclip on github
a是附件文件
tempfile = Tempfile.new("#{a.filename}", "#{Rails.root.to_s}/tmp/")
tempfile << a.body
tempfile.puts
attachments.build(
:attachment => File.open(tempfile.path)
)
最佳答案
最好确保您的临时文件具有正确的扩展名,这样您就不用再尝试更改它了:
file = Tempfile.new(['hello', '.jpg'])
file.path # => something like: "/tmp/hello2843-8392-92849382--0.jpg"
更多信息请参见:http://apidock.com/ruby/v1_9_3_125/Tempfile/new/class
关于ruby-on-rails - Rails 3 - 临时文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357174/