Search code examples
ruby-on-railslink-to

link_to file with whitespaces in name


How should I link to a file in my Rails public folder, if the filename has spaces in it? i.e.:

<%=link_to 'Download my file', root_url + "files/my file.zip"%>


Solution

  • You can use URI.escape it will take care of escaping the string for you.

    URI.escape("files/my file.zip")
    => "files/my%20file.zip"