How to save temporary files on request in Rails?

In the Rails application, users can download Excel files. My model has an ImportFile class that uses attachment_fu as follows:

class ImportFile < ActiveRecord::Base
  has_attachment :storage => :file_system, :path_prefix => 'public/imports', :max_size => 10.megabytes
end

When the user clicks "Add file", he enters a page with the field <% = fields.file_field: uploaded_data%>. attachment_fu does the job and the file loads (let's omit the verification problems). I want to save this file in the future, so I copy the downloaded file to another temp file. Temp file works fine - I see it on disk.

def self.write_to_tmp(data)
    temp_file = Tempfile.new("import", "#{Rails.root}/tmp")
  begin
    temp_file.write(data)
  ensure
    temp_file.close(false)
  end
  temp_file
end

, , , - . , , , , . .

, ? , , ? , .

+3
1

"", , Paperclip . .

, . , cron rake , .

. Paperclip , , # 903132 ... /90/31/32, .

, , , . .

+1

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


All Articles