alamodey
alamodey

Reputation: 14930

Storing pdfs in Rails app

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?

Upvotes: 4

Views: 2979

Answers (3)

Dominic Rodger
Dominic Rodger

Reputation: 99801

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.

Upvotes: 3

phillc
phillc

Reputation: 7461

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).

Upvotes: 1

Stephen ODonnell
Stephen ODonnell

Reputation: 4466

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.

Upvotes: 5

Related Questions