Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip path/url using values of an object

I've been playing around with using Paperclip to build a photo gallery/store. A Gallery has many Photos, and a Photo belongs to a Gallery, and Users can have many Galleries. The paperclip defaults do something like /:class/:style/:basename.:extension. However, with a gallery setup, I'd much rather have something like /:class/:user_name/:gallery_name/:styles/:basename.:extension. I haven't yet found a way to access variables in an object in order to dynamically create these storage locations.

Is there any way of doing this?

I've tried using #{variable} in the path, but that doesn't work. These photo objects are being created using @gallery.photos.build, so the gallery_id should already have a value that's accessible.

like image 785
Josh Kovach Avatar asked Dec 11 '10 20:12

Josh Kovach


2 Answers

Take a look at the tips and updates section on Thoughtbot.com. It discusses how to add your own interpolated variables into the path/url.

like image 118
zetetic Avatar answered Oct 26 '22 22:10

zetetic


@zetetic's answer is a bit dated (the blog post is from 2008) The current (2015) way to create custom interpolations is described in the paperclip wiki. So for the user_name in the question, probably something like this:

# interpolate in paperclip
Paperclip.interpolates :user_name  do |attachment, style|
  attachment.instance.gallery.user.name
end
like image 41
Thilo Avatar answered Oct 26 '22 22:10

Thilo