0

I'm trying to show a list of links (in index.html) to text files in a directory, for downloading the file. Where there is a subdirectory, I want its link to refresh the index page and list the files in that subdirectory (as links for download).

My file_transfers/index.html.erb has:

<%- output_dir ||= 'outputs/' %>

<%- Dir.foreach(output_dir) do |f| %>
  <%= link_to f, output_dir + f %>
<%- end %>

My last route in config/routes.rb is:

  match ':file_dir/:file_name' => 'file_transfers#show' 

Controller:

  def show
    file = params[:file_dir] + '/' + params[:file_name]
    file += ".#{params[:format]}" if params[:format]
    if File.directory?(file)
      render :index, :locals => {:output_dir => file + '/'}
    else
      send_file params[:file_dir] + '/' + params[:file_name], :type => "text/csv"
      render :index
    end
  end

When I click a directory link it does indeed refresh the list with its files, but the links show an extra 'output/' dir in the path when I hover over them, yet the href attr in the html does not have the extra path. It's like the extra folder is taken to be part of the root 0.0.0.0:3000 (as in 0.0.0.0:3000/outputs)

rigyt
  • 2,219
  • 3
  • 28
  • 40
  • 1
    Make sure you understand the risks: http://guides.rubyonrails.org/security.html#file-downloads see section 4.4. – jdoe Jun 14 '12 at 13:25

1 Answers1

0

Using redirect_to :action => :index instead of render was the solution

rigyt
  • 2,219
  • 3
  • 28
  • 40