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.

Thanks!

Answers


Use the file_field helper in your form, then in your controller you can use File.Write and File.read to save the file.

E.g. View

<%= form_for @ticket do |f| %>
  <%= f.file_field :uploaded_file %>
<% end %>

Controller

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.


Need Your Help

SQL Server 2008 - using local variables for an INSERT and UPDATE trigger

sql-server sql-server-2008 triggers

I've been tinkering away at this problem for a while, but nothing's working for me.

LDAP Searching with # character

java java-ee ldap

I am trying to search for the Organization Unit on Active Directory (Window Server 2003). This is my code

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.