如何使用回形针重命名文件上传?

时间:2013-07-05 03:56:33

标签: ruby-on-rails ruby paperclip image-uploading

我正在尝试使用paperclip重命名我上传到输入上的数据值的图像。这是我的代码:

class Deposito < ActiveRecord::Base
  attr_accessible :banco, :fecha, :monto, :rafaga, :cheque

  has_attached_file :cheque, :styles => { :medium => "800x600>", :thumb => "100x100>" }, 
                :url  => "/assets/depositos/:id/:style/:basename.:extension",
                :path => ":rails_root/public/assets/depositos/:id/:style/:basename.:extension"

  validates_attachment_content_type :cheque, :content_type => ['image/jpeg', 'image/png', 'image/gif']

end

我需要图像的名称是银行,日期,爆发,金额的值。

谢谢。

1 个答案:

答案 0 :(得分:2)

试试这个:

class Deposito < ActiveRecord::Base
  attr_accessible :banco, :fecha, :monto, :rafaga, :cheque

  has_attached_file :cheque, :styles => {:medium => "800x600>", :thumb => "100x100>"},
    :url => "/assets/depositos/:id/:style/:normalize_basename.:extension",
    :path => ":rails_root/public/assets/depositos/:id/:style/:normalize_basename.:extension"

  validates_attachment_content_type :cheque, :content_type => ['image/jpeg', 'image/png', 'image/gif']

  Paperclip.interpolates :normalize_basename do |attachment, style|
    attachment.instance.normalize_basename
  end


  def normalize_basename
    # put your filename here
  end
end