Create a unique file path using Polymorphic Paperclip

I am having a problem with different users uploading files with the same name that are being overwritten by the Polymorphic Paperclip plugin. What I would like to do is enter the current user id in the URL / path. Is it possible? Would I be better off generating a random name?

Here are my current values: url and: path in asset.rb:

:url => "/assets/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/:id/:style/:basename.:extension"

What I would like to do is the following:

:url => "/assets/#{current_users_id}/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/#{current_users_id}/:id/:style/:basename.:extension"
+3
source share
3 answers

Use bond interpolation:

config / initializers / paperclip.rb file:

module Paperclip
  module Interpolations
    def user_id(attachment, style)
      current_user.id
    end
  end
end

has_ attach_file:

:url => "/assets/:user_id/:id/:style/:filename"

( Paperclip 2.x 2.3;: ; , .)

+6

, random , , GUID. , .

0

bypaperclip.rb, :

:

class Micropost < ApplicationRecord
  Paperclip.interpolates :user_id do |attachment, style|
    attachment.instance.user_id
  end

 has_attached_file  :pic1, 
    :url => "/Microposts/:user_id/:style/:basename.:extension"

Paperclip, : gem. :

$ gem env

"- GEM PATHS:" :

:/usr/local/lib/ruby/gems/2.4.0/gems/paperclip-5.0.0/lib/paperclip

"paperclip.rb".

0

Source: https://habr.com/ru/post/1712920/


All Articles