1

I would like to display all the filenames of the files users have uploaded to my Ruby-on-Rails app in a view for admin to see the filename before downloading the file. All the files are stored in a directory.

My UploadController is the following:

class UploadController < ApplicationController

  def index

  end

  def show

  end

  def uploadFile
    uploadedFile = params[:upload][:file]
    File.open(Rails.root.join('public','files', uploadedFile.original_filename), 'wb') do |f|
        f.write(uploadedFile.read)
    end
    redirect_to upload_path, :notice => "Thanks for your upload!"
  end

  def downloadFile
    fileName = params[:download][:file].original_filename
    send_file Rails.root.join('public','files', fileName)
  end   
end

show.html.erb (the view I want Admin to be able to see uploaded filenames and download the files) is the following:

<h1>Download a file</h1>
<%= form_for :download, url: upload_admin_downloadFile_path do |f| %>
    <%= f.text_field :fileName %><br/>
    <br/>
    <%= f.submit :DOWNLOAD! %>
<% end %>

This is my upload index.html.erb

<div class="upload_container">
            <h1>- UPLOAD A FILE -</h1>
            <p>  Here you can upload files that you want to share </p>
        <%= form_for :upload, url: upload_uploadFile_path do |f| %>
            <%= f.file_field :file, :class=> "btn-uploadoldschool" %>
            <br/>
            <br/>
            <br/>
            <%= f.submit :UPLOAD!, :class=> "btn-uploadoldschool" %>
        <% end %>
        <% if user_signed_in? %>
            <% if current_user.has_role? :admin %>
                <%= link_to upload_admin_url do %>  

                <% end %>
            <% end %>
        <% end %>        

</div>  

All the help is very much appreciated! Thanks, Fox.

Fox
  • 11
  • 2
  • `show.html.erb` should be used for showing just one file, `index.html.erb` should be user for listing all the files and that is where you should be showing the file name, do you have an index.html file? and how are you displaying the list of files, are you reading them from a directory or from a database? – Subash Jan 16 '18 at 22:45
  • Thanks for your response! I added my code in index.html.erb file. All the files are stored in a directory public/files. – Fox Jan 17 '18 at 23:21

0 Answers0