回形针-保存前重命名文件 [英] Paperclip - rename file before saving

查看:71
本文介绍了回形针-保存前重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这种方法在保存之前重命名图像:

I use this method for renaming the image before the saving:

  def rename_avatar
    self.avatar.instance_write :file_name, Time.now.to_i.to_s
  end

  before_post_process :rename_avatar

该图像已按当前时间重命名,但未添加文件类型,而不是仅保存 1334487964.. 1334487964.jpg .

The image is renamed by the current time, but there's not added the file type, instead of 1334487964.jpg is saved only 1334487964..

我在那儿想念什么?我以为:文件名仅包含文件名-没有文件类型

What I missing there? I thought :file_name contains only the file name - without the file type

推荐答案

这是解决问题的方法:

  def rename_avatar
    #avatar_file_name - important is the first word - avatar - depends on your column in DB table
    extension = File.extname(avatar_file_name).downcase
    self.avatar.instance_write :file_name, "#{Time.now.to_i.to_s}#{extension}"
  end

这篇关于回形针-保存前重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆