Clip path / URL using object values

I played using Paperclip to create a photo gallery / store. There are many photos in the Gallery, and the photo belongs to the gallery, and users can have many Gallery. The default options for paperclip do something like /:class/:style/:basename.:extension. However, with the gallery setup, I would rather want something like /:class/:user_name/:gallery_name/:styles/:basename.:extension. I have not yet found a way to access variables in an object to dynamically create these storage locations.

Is there any way to do this?

I tried using # {variable} in the path, but this does not work. These photo objects are created using @ gallery.photos.build, so gallery_id should already have an accessible value.

+5
source share
2 answers

Check out the tips and updates section at Thoughtbot.com. It discusses how to add your own interpolated variables to the path / URL.

+8
source

@zetetic answer is a bit outdated (blog post since 2008). The current (2015) way to create custom interpolations is described in the paperclip wiki . So for user_namein question, maybe something like this:

# interpolate in paperclip
Paperclip.interpolates :user_name  do |attachment, style|
  attachment.instance.gallery.user.name
end
+4
source

All Articles