使用回形针,如何将图像位置更改为“:parent_model_id /:id”文件夹格式?

鉴于我有一个包含许多图像清单模型,每个图像都有一个附件 ,我怎样才能将listing_id作为文件夹结构的一部分?

像这样:system / photos / [listing_id] /:id

我知道使用:id将输出图像记录的id。

这是我现在拥有的:

class Image  { :medium => "300x300>", :thumb => "100x100>" }, :url => "/public/system/:class/:attachment/:id/:style_:filename" 

结束

啊,我终于明白了。 我需要使用Paperclip.interpolates

思想机器人的这篇文章解释了它,但它有点过时了。

首先,创建一个config / initializers / paperclip.rb文件并添加以下内容:

 Paperclip.interpolates :listing_id do |attachment, style| attachment.instance.listing_id # or whatever you've named your User's login/username/etc. attribute end 

这意味着现在在我的图像模型中我可以参考:listing_id,如下所示:

 class Image < ActiveRecord::Base belongs_to :listing #Rails ActiveRecord Relation. An image belongs to a post. # paperclip data has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :url => "/system/:attachment/:listing_id/:id/:style_:filename" #location where to output the server. :LISTING_ID is defined in config/initializers/paperclib.rb end 

PS:您需要在initializers.rb中的更改生效之前重新启动服务器

你需要传递一个url和path属性。 看看这个thinkbot博客文章寻求帮助。 你拥有它的方式很接近,但你需要传递我认为的listing_id,而不是:id。

由于您的Image模型上有belongs_to关系,因此您应该能够将listing_id用作paperclip配置的一部分:

 has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :url => "system/photos/:listing_id/:id"