-
-
Save ELLIOTTCABLE/20725 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The simplest thing that could possibly work. Will probably overwrite files if someone uploads | |
# a file that has the same file name as an already uploaded file. Requires the columns 'size' and | |
# 'content_type', see the 'store_file' method. You could obviously just remove those calls | |
# if you don't want to store the file size and the content type in the database. | |
class Attachment < ActiveRecord::Base | |
after_create :store_file | |
# Virtual attribute that stores the uploaded tempfile | |
attr_accessor :uploaded_file | |
private | |
def store_file | |
File.open(file_storage_location, "w") do |f| | |
f.write upload.read | |
end | |
self.size = uploaded_file.size | |
self.content_type = uploaded_file.content_type | |
end | |
def file_storage_location | |
File.join(Rails.root, 'public', 'attachments', uploaded_file.original_filename) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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