我想你可以定义自己的插值
interpolation
这样做。然后可以正常附加文件。例如:
# config/initializers/paperclip.rb
Paperclip.interpolates :custom_filename do |attachment, style|
# Generate your desired file name here.
# The values returned should be able to be regenerated in the future because
# this will also be called to get the attachment path.
# For example, you can use a digest of the file name and updated_at field.
# File name and updated_at will remain the same as long as the file is not
# changed, so this is a safe choice.
SHA1.sha1("#{attachment.original_filename}-#{attachment.updated_at}")
end
# app/models/post.rb
class Post < ActiveRecord::Base
has_attached_file :attachment,
:path => ':rails_root/public/system/:class/:attachment/:id/:style/:custom_filename',
:url => '/system/:class/:attachment/:id/:style/:custom_filename'
end
请注意,这只会更改文件系统中的文件名。
model.attachment_file_name
或
model.attachment.original_filename
仍将保留原始文件名。