link_to download file from path over public

If you're using Capistrano, I suggest you place your files in:


And then create an after_symlink deployment task to symlink /path/to/your/app/current/public/files/ to your shared files directory. The RailsMachine gem does this so look there if you need the exact code.

You can then reach them in the browser at, say,

In your controller, you can reach them with, say, send_file 'public/files/your_file.txt'.

Andy Stewart

Ok but if i symlink to another directory over the public, wont other
users that arent loged in be able to download these files ?

Yes they will. I didn't realise that you wanted to restrict access.

or should i delete the symlink when logout.. ?

That's messy. Here's a better way to do it:

- Make the directory app/shared/private/ on your server. It should sit beside the existing app/shared/public/. Make a directory files/ inside it and stick your files in there.

- Using Capistrano symlink your app/current/private/ to the shared one. For example, add this to your deploy/config.rb:

# Automatically symlink these directories from current/private to shared/private.
set :app_private_symlinks, %w{ files }

desc "Link up any private directories."
   task :symlink_private, :roles => [:app, :web] do
     if app_private_symlinks
       app_private_symlinks.each { |link| run "ln -nfs #{shared_path}/private/#{link} #{current_path}/private/#{link}" }

desc "Creates additional symlinks."
task :after_symlink, :roles => [:app, :web] do

- In your action, you can do something like:

if @user.logged_in? # or however you determine whether somebody is logged into your app
   send_file 'private/files/your_file.txt'

It maybe sounds litle bit newbe, im not very skilled yet...

Hey, don't worry about it. Nor am I :slight_smile:

Andy Stewart