In my Rails application users can upload Excel files. In my model there is a class ImportFile that uses attachment_fu like this:
class ImportFile < ActiveRecord::Base
has_attachment :storage => :file_system, :path_prefix => 'public/imports', :max_size => 10.megabytes
end
When user clicks "Add file" he enters a page with a <%= fields.file_field :uploaded_data %>. attachment_fu does it's job and file upload is being done (let's ommit validation problems). I want to keep this file for future so I copy uploaded file to other temp file. Temp file is working fine - I can 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
What I want to do is to show user a preview and then let him choose if he wants to add a file or discard it - there are two buttons. I have a problem when user chooses to save a file, because a temp file I've just created above is gone. It is deleted before request.
Does anyone has hints how to achive this? Or can point me to upload-with-preview file scenario like the one I've presened? I've been looking for days, but I've failed to find one.