上传文件后,是否可以在保存模型之前打开该文件?
我正在使用Paperclip将文件保存在/public文件夹中。
class Book < ActiveRecord::Base
before_save :open_it
has_attached_file :upload
def open_it
a_file = open(upload.url) # this doesn't work before save ?
# ...
end
end
最佳答案
找到了:
def model_method
f = open(self.upload.queued_for_write[:original].url)
end
更新:
根据生态学的回应,对于较新版本的Paperclip gem,请使用
.path
而不是.url
关于ruby-on-rails - 在临时文件保存到Rails模型之前,先打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369233/