Make Dynamic Paths for Paperclip in Ruby on Rails application


For creating Dynamic paths , we need to overwrite the Paperclip::Interpolations module of paperclip and we will add a new method like user_id etc and this will return that dynamic value which will be used to create a dynamic path.

config/initializers

module Paperclip
    module Interpolations
        def user_id attachment, style_name
            attachment.instance.user_id
        end
    end
end 
 
And now we can use :user_id in our path 
 
has_attached_file :user_image
    :url => "../assets/users/:user_id/#{self.name}_:basename_:id" + ".:extension",
                  :path => ":rails_root/public/assets/users/:user_id/#{self.name}_:basename_:id" + ".:extension"
 

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array

Difference between Validations, Callbacks and Observers