Linking to external file in Ruby on Rails
I have a file I want to share as a link in my rails app.
I create a link to it in the .erb file like this
<li><a href="somefile.pdf">Some File</a> </li>
When I select the link I get the following error.
Routing Error
No route matches “/pages/somefile.pdf”
with {:method=>:get}
Do I really need a route for this? I really just want the save as dialog to popup. Just a link to a file that the user can open or download.
What is the rails way to do this (rails newbie here)?
You should place the file in the app’s
public/directory and use a forward slash at the start of the path in your link’s href.The problem you are having is because
href="somefile.pdf"is relative to the current URL which is probably something likehttp://localhost:3000/pages/42. By usinghref="/somefile.pdf"instead the resolved URL will behttp://localhost:3000/somefile.pdf(rather thanhttp://localhost:3000/pages/somefile.pdf) and it won’t conflict with your pages routes.