ruby-on-rails – 在Rails模型中保存之前打开临时文件

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在Rails模型中保存之前打开临时文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
上传文件后,是否可以在@R_69_301@之前打开该文件

我正在使用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

更新:

根据ecoologic的响应,使用.path而不是.url来获取Paperclip gem的更新版本

原文链接:https://www.f2er.com/ruby/271301.html

猜你在找的Ruby相关文章