views:

97

answers:

3

How would I store a pdf in my Rails app so that users can access/download them?

In my migration, would i i use the datatype t.binary?

Then in my controller/view, how should I present this data?

+2  A: 

The answer here is probably it depends. Depends on the size and number of PDFs, where users have to be logged in to view them etc.

If you have lots of large PDFs, its probably not a good idea to store them in the database - just store them on the filesystem and store a file location in the database model.

If you do want to store them in the database, then using a binary column is the way to go.

If users don't have to be logged in to download the PDF's, then you could just place them into the public folder (inside a subfolder) and generate links to them for download - then your controller would only need to generate a link to a static PDF file, and the front end webserver will serve them up automatically without using a Rails process.

Stephen ODonnell
+1  A: 

This question probably isn't rails specific, but probably best way to do it is to generate the pdf and store them on your filesystem, then in your database keep a record of filenames and a date of when they were created (to delete old ones if you need to).

phillc
+3  A: 

I've heard good things about Paperclip, which is a way of transparently handling this stuff - storing files on the filesystem, with a reference to that file location in the database.

Dominic Rodger