使用paperclip gem如何持久保存图像file_path而不是file_name

时间:2017-04-29 06:52:56

标签: ruby-on-rails-4 paperclip activeadmin

我正在使用paperclip gem将文件上传到S3,并且还希望将完整的s3文件路径保存在数据库的image_file_name列中。但是只有文件名存储。我的模型如下所示

class Product < ActiveRecord::Base
  self.table_name = "catalog"
  has_attached_file :image, :styles => { :medium =>     "300x300#", :thumb 
=> "200x200#" }
attr_accessible :image_file_name,:image_cache
after_post_process :basename
private
def basename
  self.image_file_name  = @product.image.url(:medium)
end

  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.
  self.image_file_name = 
    File.basename(URI.parse(attachment.original_filename).path)
  end
end

0 个答案:

没有答案