Rails 3.1: ruta / url al archivo en el directorio público

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

preguntado el 08 de noviembre de 11 a las 15:11

as far as i know, the public directory works pretty much like a static file system. So if your app sits under a folder, as supposed to a domain, you will need to append that to /downloads/ -

@Daniel If you're trying to access that URI from something other than the front-end of the Rails app, let us know, because that may also be significant. -

2 Respuestas

I think normalocity was almost right here, but it should rather be root_path que 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.

Respondido 15 Feb 13, 20:02

exactly. i've had root_url, but root_path is imho more logical for this helper. thank you! - Daniel

@Frost, I think you might be missing an equals sign right after your first percent symbol. Without that equals sign, nothing will be displayed. - Tass

@Tass Thank you for noticing. I just fixed it. - Escarcha

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

Respondido 19 Oct 16, 12:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.