Linking to external file in Ruby on Rails
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 开发者_StackOverflow社区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 like http://localhost:3000/pages/42
. By using href="/somefile.pdf"
instead the resolved URL will be http://localhost:3000/somefile.pdf
(rather than http://localhost:3000/pages/somefile.pdf
) and it won't conflict with your pages routes.
You could also just do <%= Link_to "name of link", "http://pathoffilehere.pdf" %>
Don't forget the quotes.... :)
精彩评论