RAILS:回形针和基于创建日期的目录结构

Ale*_*uer 2 interpolation ruby-on-rails date attachment paperclip

有谁知道我如何配置使用回形针进行数据存储的Rails模型,以使用基于创建日期的目录,例如在fleximage中?

目前,我正在使用:

has_attached_file :bookblock, :path => "#{CONF['storage_path']}bookblock/:id_partition/:style.:content_type_ehas_attached_filextension"
Run Code Online (Sandbox Code Playgroud)

但是我需要的是这样的东西

has_attached_file :bookblock, :path => "#    {CONF['storage_path']}bookblock/:created_at_year/:created_at_month/:created_at_day/:c:id_partition/:style.:content_type_ehas_attached_filextension"
Run Code Online (Sandbox Code Playgroud)

在目录路径中一个简单的:created_at也会有帮助

{CONF['storage_path']}/:created_at/bookblock/:id_partition/:style.:content_type_ehas_attached_filextension"
Run Code Online (Sandbox Code Playgroud)

提前感谢,

亚历克斯

Ari*_*jan 5

您可以将自己的插值添加到回形针。举一个简单的例子:

Paperclip.interpolates :year do |attachment, style|
  attachment.instance.created_at.year
end
Run Code Online (Sandbox Code Playgroud)

现在,您可以:year在这样的:path选项中使用:

has_attached_file :bookblock, :path => "#{CONF['storage_path']}bookblock/:year/:id/:style.:content_type_ehas_attached_filextension"
Run Code Online (Sandbox Code Playgroud)

你可以定义三个插值::year:month:day,或者只是一个返回整年/月/日的字符串。