Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails link to file in public folder

I have a file data.txt in the public folder of my Rails project. I want to link to it from one of the pages in my project using link_to. How can I do it?

If I do

<%= link_to "here", "data.txt" %>

it links to domain.com/data.txt instead of domain.com/my_project/data.txt.

like image 821
Paul S. Avatar asked May 29 '13 02:05

Paul S.


3 Answers

This worked for me:

<%= link_to 'Sitemap', root_url+'my_sitemap.svg', { target: '_blank' } %>
like image 133
Chris Avatar answered Sep 24 '22 01:09

Chris


It sounds like you want to access the file here:

<%= link_to "here", "/my_project/data.txt" %>
like image 26
Mark Swardstrom Avatar answered Sep 25 '22 01:09

Mark Swardstrom


I'd go with:

link_to 'Download sample', root_path + 'data/invoices/sample.pdf'

Update 2018-08-09

  • Performance does not matter if you generate single link (benchmark it if you many of them)
  • Probably "#{root_path}data/invoices/sample.pdf" is much simpler
like image 40
Artur Beljajev Avatar answered Sep 23 '22 01:09

Artur Beljajev