Topic: Noob: visitors to download pdf file from static site created in Rail3?

After well over 6 hours of pretty satisfying but fruitless work I'm finally crying mama and throwing myself on the mercy of this august forum.  I can't figure out how to arrange for visitors to a currently completely static webpage created in RoR3 can download a pdf file from it.  Anyone have any leads?

I've tried storing the file in lib/ and public/  -- there is no need for any type of protection for the file.

I've tried just using HTML on the page where the download should occur.  Things like:

<a href="/public/[filename].pdf"></a>
or
<a href="[rails root directory]/[app name]/public/[filename].pdf"></a>
which generates a link but produces a "Routing Error No route matches "/public/[filename].pdf"

I've tried things like:
views:
<%= link_to 'Download PDF', '/public/champhandbook.pdf' %>
which generatesthe same routing error.

I've also tried:
views:
<%= button_to "Download File", :action=>'pdf_download'%>

controller:
  def pdf_download
    send_file(public/champhandbook.pdf)     # or send_file(/public/champhandbook.pdf)
  end

This seems to mess up the routes to the page with the "download pdf" link on it.
I think that I should probably add something to the config/routes.rb file though I'm not sure what.  I've tried 'get /[file path] and different iterations of 'match#[ ] but nothing seems to work.  Forgive me if this is very very basic.

If anyone has any thoughts, I'd be super grateful! 

Basically I'm 8 chapters in to Michael Hartl's excellent Ruby on Rails Tutorial.  My brother has a static webpage -- literally one page -- where I arranged for people to download a free ebook pdf file he wrote.  I figured that since its current hosting is about to expire, why not spread its current single page form over several pages and apply and reinforce some of the little Rails I've picked up.  I'm pretty much starting from very little knowledge.  Over the weekend I got most of the site up and it looks pretty good.  My brother's happy.  I'm completely stuck with putting up a link where people can download the file, the raison d'être of the whole site.

Last edited by BenU (2011-01-11 22:55:12)

Re: Noob: visitors to download pdf file from static site created in Rail3?

Can't you have them view it and then download it or right-click and save it?

If I put the PDF in \public\images I can get it using:

<%= link_to "Download", image_path('test.pdf') %>

If I put the PDF in \public I can get it using:

<%= link_to "Download", '/test.pdf' %>

Hope that helps.

Re: Noob: visitors to download pdf file from static site created in Rail3?

just create a folder named "pdf" (or what you want) in public (%APP_ROOT%/public/pdf).

#helper
def pdf_path(file)
  File.join('/pdf', file).to_s
end

#view
<%= link_to 'pdf', pdf_path('test.pdf') %>

I don't understand the storage of the file in /lib (????)

Last edited by Mulasse (2011-01-12 07:10:13)

Re: Noob: visitors to download pdf file from static site created in Rail3?

Thanks a mill, jmesserer.  Both of those ways work!  Now I'm on to explore Mulasse's approach as well.  There's so much to learn and understand... Much positive karma sent both your ways!

Last edited by BenU (2011-01-12 19:17:43)

Re: Noob: visitors to download pdf file from static site created in Rail3?

Thank you Mulasse.
You've helped another RoR Noob
Learning fast...
Skrik