How to support file downloads (e.g., PDFs, .docs)

I have a simple website in which I upload files (e.g., PDFs
and .docs) for later download by visitors to the site. I store the
files under public/images in a sub-directory corresponding to the
particular file. An example location might be

public/images/books/#{toc_dir}/#{toc_name}

where toc_dir = “Book_TOC/toc” and toc_name = “book.pdf”

In my index.rhtml file I use the following code to create a link to
the above resource:

<% if book.toc_file_name != nil %>
<% toc_name = book.toc_file_name %>
<% toc_dir = “#{book.title.gsub(/[^a-zA-Z0-9.]/, ‘_’)}/toc” %>
<%= link_to(“TOC”, “/images/books/#{toc_dir}/#{toc_name}”, :popup =>
true) -%>
<% end %>

In my test environment (Windows XP, InstantRails) this is works just
fine but when I upload to the production environment (Linux/CentOS)
clicking the link results in the following complaint:

no route found to match “/images/books/Book_TOX/excerpt/book.pdf” with
{:method=>:get}

What is the proper way to deliver files. I tried send_file but
couldn’t get that working. I also tried an absolute path in the
link_to command but that resulted in the same route complaint. Is it
necessary to put the files in a directory that is not under public/
images?

Any help is appreciated.

  • SD

most likely you are linking to a non-existent file.

The fcgi/mongrel process shouldn’t even be picking up the request.
Either the file doesn’t exist (at that location) or you have massively
mis configured your server

you should consider using file_column plugin