2

我正在使用 Rails4 和 mongoid-paperclip

mongoid-paperclip (0.0.9)
      paperclip (>= 2.3.6)

我想更改文件名,所以我的课程如下,

class Icon
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paperclip

  has_mongoid_attached_file :icon,
    :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
    :url => "/system/:attachment/:id/:style/:filename",
    :styles => {
      :small => { :geometry => "100x100"
    }
  }

  before_post_process :rename_avatar

  private

  def rename_avatar
    extension = File.extname(icon_file_name).downcase
    self.icon.instance_write(:file_name, "#{extension}")
  end

end

如何实现这个!!!

4

1 回答 1

0

是的,我没有提到文件名。

所以修改的方法是,

def rename_avatar
    extension = File.extname(icon_file_name).gsub(/^\.+/, '')
    filename = icon_file_name.gsub(/\.#{extension}$/, '')
    self.icon.instance_write(:file_name, "#{filename}.#{extension}")
end
于 2014-04-02T09:45:57.260 回答