在model中:

has_attached_file :photo,
:styles => { :small => '200x200>' },
:withy => false,
:url => "/p_w_uploads/:p_w_upload/#{curr_date}/:style/:normalized_photo_file_name",
:path => ":rails_root/public/p_w_uploads/:p_w_upload/#{curr_date}/:style/:normalized_photo_file_name"

Paperclip.interpolates :normalized_photo_file_name do |p_w_upload, style|
p_w_upload.instance.normalized_photo_file_name
end

def normalized_photo_file_name
"#{self.photo_file_name.gsub( /[^a-zA-Z0-9_\.]/, '_')}"
end


添加如下内容,主要是为了重新命名文件
before_create :create_random_file_name

def create_random_file_name
extension = File.extname(photo_file_name).downcase
self.photo.instance_write(:file_name, "#{Time.now.strftime("%Y%m%d%H%M%S")}#{rand(10000)}#{extension}")
end

 

转载自:http://www.oschina.net/question/92992_52302