sabrams
sabrams

Reputation: 1128

Open a .pdf file with Rails outside the public folder

I'm new to the programming world and I'm working with Rails to create my first project. I have a library of .pdf files saved on a separate shared network server. I would like to make links to some of these files so that when I click the link, the pdf opens in the browser.

I understand that by using:

<a href="/filename.pdf">Click Me</a>

I can open a file in the /public folder in my rails project, but I'm looking to open a file on a completely different harddrive. I've tried

`href="//drivename/folder/nextfolder/filename.pdf" `

but the browser gives me a 404 error saying file not found.

Upvotes: 3

Views: 3791

Answers (1)

Muntasim
Muntasim

Reputation: 6786

Better use a controller action to serve the file:

say you have pdfs controller,

in view:

<a href="pdfs/serve/filename">Click Me</a>

in controller

def serve
  path = "/drivename/folder/nextfolder/#{params[:filename]}"

  send_file( path,
  :disposition => 'inline',
  :type => 'application/pdf',
  :x_sendfile => true )
end 

Upvotes: 7

Related Questions