Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1: path/url to file in public directory

I've got a bunch of files in public/downloads/ directory now how do I get url for them?

P.S. doing just /downloads/xyz.tar.bz2 won't do, because app can be deployed to sub URI.

UPD I've found a dead simple solution:


  def public_url(file)
    root_url + file
  end
like image 742
Daniel Avatar asked Nov 08 '11 15:11

Daniel


2 Answers

I think normalocity was almost right here, but it should rather be root_path than Rails.root.

<%= link_to "The file", root_path+"/downloads/test.txt" %>

That should match whatever sub-uri your app resides under, and then append the path to the static file to it.

like image 102
Frost Avatar answered Nov 16 '22 15:11

Frost


link_to "The file", asset_path("downloads/test.txt")

  • asset_path Computes the path to asset in public director since Rails 4, I believe
  • asset_path documentation
like image 36
Robert Pankowecki Avatar answered Nov 16 '22 15:11

Robert Pankowecki