回形针使用参数在:url选项



我有两个模型:照片和属性。一处房产有许多照片(_M)。当通过回形针上传照片时,我希望目录结构类似于properties/the_property_id/:id。:style(当然,第二个:id这里应该是图像id,因为这就是模型所在的位置)。

通过属性模型(accepts_nested_attributes_for)添加和编辑照片。因此,在查看属性编辑页面时,我需要一种将属性id(params[:id])传递给照片模型的方法,这样回形针就可以使用类似于:url=>properties/params[:id]/:style/:id.:extension的东西。(属性/6/small/2.jpg)

正在寻求最佳方法的建议。。。你将如何完成这项任务?

您需要使用Paperclip插值(请参阅https://github.com/thoughtbot/paperclip/wiki/Interpolations详细信息)。然后你可以有这样的东西:

Paperclip.interpolates :param_value do |attachment, style|
  attachment.instance.property.params[attachment.instance.id]
end

并以这样的方式在您的模型中使用它:

:url => properties/:param_value/:style/:id.:extension

最新更新