Paperclip how to change basename (filename)?

前端 未结 4 2715
粉色の甜心
粉色の甜心 2021-02-20 15:31

I am trying to change the basename (filename) of photos:

In my model I have:

  attr_accessor :image_url, :basename

  has_attached_file :image,
                  


        
4条回答
  •  别那么骄傲
    2021-02-20 16:09

    Im doing this to strip whitespaces:

    before_post_process :transliterate_file_name
    
    private
    def transliterate_file_name
      self.instance_variable_get(:@_paperclip_attachments).keys.each do |attachment|
        attachment_file_name = (attachment.to_s + '_file_name').to_sym
        if self.send(attachment_file_name)
          self.send(attachment).instance_write(:file_name, self.send(attachment_file_name).gsub(/ /,'_'))
        end
      end
    end
    

    I hope this will help you.

    edit:

    In your example:

    def basename
      self.image_file_name = "foobar"
    end
    

    Should do the job. (but might rename the method ;) )

提交回复
热议问题