加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:32:45 所属栏目:百科 来源:网络整理
导读:上传文件后,是否可以在保存模型之前打开该文件? 我正在使用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 sa
上传文件后,是否可以在保存模型之前打开该文件?

我正在使用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的更新版本

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读