What if you want to upload a file to your app? You can use the attachment_fu plugin.
The model code needs filename and size and perhaps other fields depending on your implementation, surf over to techno-weenie for info on what attachment_fu supports. This example assumes the model is named ImportProject.
fields do
content_type :string
filename :string
size :integer
timestamps
end
has_attachment :storage => :file_system
, :max_size => 100.megabytes
, :path_prefix => "public/import_projects"
validates_as_attachment
To get the plugin from the git repository change directory over to the plugins directory clone from git.
git clone git://github.com/technoweenie/attachment_fu.git
You may need this patch:
create a file appname/lib/attachment_fu_patch.rb
require 'tempfile'
class Tempfile
def size
if @tmpfile
@tmpfile.fsync # added this line
@tmpfile.flush
@tmpfile.stat.size
else
0
end
end
end
And this needs to be added to: appname/config/environment.rb
require 'lib/attachment_fu_patch.rb'
environment.rb
# Be sure to restart your server when you modify this file
# Uncomment below to force Rails into production mode when
# you don't control web/app server and can't set it the proper way
# ENV['RAILS_ENV'] ||= 'production'
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.1.0' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
require 'lib/attachment_fu_patch.rb'
Rails::Initializer.run do |config|
...
How about some view code? appname/app/views/import_project/new.dryml
<%= error_messages_for :import_project %>
<new-page>
<form: enctype="multipart/form-data">
<label>Choose File</label>
<input type="file" name="import_project[uploaded_data]"/>
<submit label="Upload File"/>
</form:>
</new-page>
When you create a new import project you should have an upload button to select the file for uploading.
User contributed notes
-
On November 06, 2008 konung said:
Hi Robi.
I can't really comment on the use of this plugin for your purpose, but here is a DRY question - why would you want to store files in a DB in the first place, it's been already implemented in a file system much better? There are only cons:
- performance hit
- depending on your DB you maybe locking not just a record, but a whole table while you are inserting your binary file
- exponentially larger and longer db backups (especially if the DB server is a remote server)
- hard to do incremental backups
- DBs as a rule of thumb are a bad solution to store binary data - storage space is cheaper than DB space on most hosts.
- it's much easier to grow file storage space, than growing your DB space (even if you running an intranet project)
- it's easier to implement document search solution on a file/folder system, then on binary data in a DB.
The only "pro" that's quoted in many places is that it's easier to manipulate file and get file info if you store it in a DB. Well not really a pro - ruby provides plenty of tools for these tasks.
I'm not trying to be a smarty pants, but just trying to save you some aggravation down the line ( I recently had to deal with a bloated support ticket DB, where people kept saving screenshots, drivers and software as binary data in a DB - some people were saving 68mb service packs as attachment to a ticket for clients to download - original programmer set php.ini upload limit to 100mb, and saved everything in MySQL) -
On November 07, 2008 robi said:
Hi Konoung - thanks for taking the time to explain your point of view and experience. I really appreciate this type of insight. => I will deploy to the file system! -
On November 11, 2008 brett said:
robi - It probably wouldn't delete the file when the record is deleted, but maybe there is an after_delete hook like the before_create hook in the model that could be used for this.
Agree with konung about storing to DB instead of filesystem. If you are using a linux fileserver you really can't beat the performance of its filesystem.
Also the plugin creates a lot of subdirectories when it stores files and I had to use a file search to then locate the uploaded file.
Brett -
On October 09, 2009 sanjayayogi said:
The directions worked well to create the ability to upload a file, thanks.
A couple of questions:
How would you then create and display a link to the file to then download it?
Would it be possible to create a thumbnail and display that image as a link? -
On December 11, 2009 Sam said:
Hi Brett,
I am trying to upload an image file. It comes with an error saying "unknown method 'content_type for C:/filename :string" and it is pointing towards attachment_fu.rb
Do I miss something? Thanks
Sam

On November 04, 2008 robi said:
Great recipe - thank (I was about to look at paperclip, but will try this first). If you delete a record does it delete the corresponding file?Does this work if you store the file in the database - or does it become unstable?