Ruby on rails 4 使用回形针gem如何保存图像文件路径而不是文件名
我正在使用回形针gem将文件上载到S3,还想将完整的S3文件路径保存在数据库的image\u file\u name列中。但是只存储文件名。我的模型如下所示Ruby on rails 4 使用回形针gem如何保存图像文件路径而不是文件名,ruby-on-rails-4,paperclip,activeadmin,Ruby On Rails 4,Paperclip,Activeadmin,我正在使用回形针gem将文件上载到S3,还想将完整的S3文件路径保存在数据库的image\u file\u name列中。但是只存储文件名。我的模型如下所示 class Product < ActiveRecord::Base self.table_name = "catalog" has_attached_file :image, :styles => { :medium => "300x300#", :thumb => "200x200#" } at
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
类产品{:medium=>“300x300#”,:thumb
=>“200x200#”}
属性可访问:映像\u文件\u名称,:映像\u缓存
后处理:basename
私有的
def基本名称
self.image\u file\u name=@product.image.url(:medium)
结束
曲别针.interpoles:自定义文件名do |附件,样式|
#在此处生成所需的文件名。
#返回的值应该能够在将来重新生成,因为
#这也将被调用以获取附件路径。
#例如,您可以使用文件名摘要和更新的_at字段。
#只要文件不存在,文件名和更新的位置将保持不变
#改变了,所以这是一个安全的选择。
self.image\u文件名=
File.basename(URI.parse(attachment.original_filename).path)
结束
结束