Skip to content

Instantly share code, notes, and snippets.

@maxdevel
Forked from augustl/application_helper.rb
Created December 25, 2008 18:58
Show Gist options
  • Save maxdevel/39935 to your computer and use it in GitHub Desktop.
Save maxdevel/39935 to your computer and use it in GitHub Desktop.
# The simplest thing that could possibly work.
class Attachment < ActiveRecord::Base
before_create :store_metadata
after_create :store_file
# Virtual attribute that stores the uploaded tempfile
attr_accessor :uploaded_file
private
def store_metadata
self.size = uploaded_file.size
self.content_type = uploaded_file.content_type
end
def store_file
File.open(file_storage_location, "w") do |f|
f.write uploaded_file.read
end
end
def file_storage_location
File.join(Rails.root, 'public', 'attachments', uploaded_file.original_filename)
end
end
<% form_for @attachment, :multipart => true do |f| %>
<%= f.file_field :uploaded_file %>
<%= f.submit "Upload file" %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment