rails paperclip customize upload url path -
this upload.rb
class upload < activerecord::base belongs_to :post has_attached_file :upload,styles: { medium: ["500x400>",:jpg], thumb: ["100x100>",:jpg]},url: "/post_images/post_:postid/:style/:filename" def postid self.post_id end end
i have column post_id. belongs_to
represents have more 1 images 1 post. while saving files in folder instead of post_25
. storing post_:postid
but if give :id
working.
how can solve it. can this.
you should using paperclip's interpolations achieve functionality. first, start defining interpolation in initializer:
# config/initializers/interpolations.rb paperclip.interpolates :postid |attachment, style| 'post_' + attachment.instance.post.id end
then, can use :postid
interpolation directly in attachment url declaration (remember restart server first):
# app/models/upload.rb has_attached_file :upload,styles: { medium: ["500x400>",:jpg], thumb: ["100x100>",:jpg]},url: "/post_images/:postid/:style/:filename"
note :postid
not merely instance method you've defined in model – paperclip exclusively utilizes interpolations define dynamic variables within url/path declarations.
Comments
Post a Comment