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 开发者_StackOverflow社区should already have a value that's accessible.
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.
@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
精彩评论