How to upload a file temporarily in Rails 3?
I'm creating CSV-upload functionality for a site of mine. I'm looking to upload a file, parse it, and then dispose of it.
I know I can upload and save a file using Paperclip, but that seems a bit like overkill.
All I need to do is parse the uploaded file and never save it. How would I go about doing this in Rails 3?
Note: I'd prefer to do the uploading manually without using an external gem so I can learn how to process works, but any suggestions are welcome.
Use the file_field helper in your form, then in your controller you can use File.Write and File.read to save the file.
<%= form_for @ticket do |f| %> <%= f.file_field :uploaded_file %> <% end %>
def upload uploaded = params[:ticket][:uploaded_file] File.open(<insert_filename_here>, 'w') do |file| file.write(uploaded.read) end end
Edit: Just saw @klochner's comment, that link says pretty much what I have said so follow that: RubyOnRails Guides: Uploading Files.