Paperclip回调before_post_process在Rails4中不起作用

时间:2014-04-02 05:23:04

标签: ruby-on-rails-3 ruby-on-rails-4 paperclip mongoid4

我正在使用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

如何实现这一点!!!

1 个答案:

答案 0 :(得分: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