Paper clip for paper clip

Is there a better way to save some line as an attachment using Paperlip, how to make a tmp file, insert a line into it, open it again and save it as an attachment?

Like this:

  def save_string data
    tmp_file = "/some/path"
    File.open(tmp_file,'w') do |f|
      f.write(data)
    end

    File.open(tmp_file,'r') do |f|
      ceneo_xml = f
      save!
    end
  end
+3
source share
2 answers

Paperclip stores the files along with your models - this is what was written for this, so I think the short answer is no.

If you look in attachment.rbat the source of the paper clip, you will see a method called def assign uploaded_file. If you look at the implementation of this method, you will see that it expects the loaded file object to have certain methods defined on it.

, , Paperclip, , , Paperclip - , , .

0

- StringIO, , - . , StringIO,

class InvoiceAttachment < StringIO
 def initialize(invoice, content)
   @invoice = invoice
   super(content)
 end

 def original_filename
   from = @invoice.from
   to = @invoice.to
   date = @invoice.created_at.strftime('%B-%Y').downcase 
   "invoice_#{date}_from_#{from}_to_#{to}.pdf"
 end

 def content_type
   'application/pdf'
 end
end

!

+4

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


All Articles