views:

58

answers:

4

I've only just started learning ruby on rails and I would like to create an application that will allow me to add files to the database. Currently, I'm developing the rails application using the Aptana plugin for Eclipse and the application is using the default sqllite db.

I have tried generating a scaffold with the following parameters: documents title:string file:varbinary. Then I do a 'rake'->'db'->'migrate'. When I migrate to localhost/documents and click on 'New Document' the application fails and displays an error.

What I would like to do is click on 'New Document', have a field that will allow me to browse for a document on my local computer, select the document and add it to the db on the rails application.

+1  A: 

Is it a particular kind of file you want to add?

I just ask because if it's not data of a kind that benefits from being in a database ( textual data might be searchable, binary data is not ) then you are much better storing it in the filesystem and serving it up straight - especially for stuff like images or video - rather than inserting it into a database and having to go through your application every time a user requests it.

I'm not saying that there aren't any reasons you might want to have a file in the database, but I treat that as a last resort and in ten years of web programming I've not come across a case where it was necessary.

glenatron
we are trying to implement a document management system, which implements the company process. Therefore the document needs to be stored in a central repository of somesort, and it must exist in states that can only be accessed by certain users at certain times. With that amount of control required - I thought it might only be possible using a db?
Seth
You can actually implement that in a few ways- if the data is stored in an area of the filesystem only accessible by the user running rails and sysadmins then you can do whatever you want in terms of access control as requests for the files have to come through your application, so you could store permissions details in the database and only serve the files up to permitted users through your application layer. Or you could use permissions on the filesystem and manage it that way- possibly easier for your sysadmins to work with.
glenatron
+1  A: 

I would highly recommend the attachment_fu plugin as this lets you create models with attachments pretty nicely, Paperclip plugin is another good one also!

If you have trouble deciding which one to use, as far as i can remember, Paperclip makes it easier for multiple attachments, such as an Album has many Photos, and Attachment_fu is easier for single attachments such as a User has one display picture.

cast01
excellent. Found a good tutorial here: `http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu`
Seth
+4  A: 

Paperclip is more recommended than attachment_fu these days. It is really simple and easy to use with your active record model.

jpartogi
+1  A: 

We do something like this on a site I'm managing. Instead of storing these files in a database, I'd agree with the other posters here and recommend you try something like Paperclip.

One caveat: if you want access control, make sure that paperclip doesn't save your files somewhere under /public, where anyone could possibly access them if they knew the URL. Deliver files to the user via send_file in your controller.

Cratchitimo