dannymcc
dannymcc

Reputation: 3814

Listing files from a remote directory with Rails 3?

I have a samba server on our network which contains a number of directories, one of the directories is full of .xls Microsoft Excel documents.

What I am trying to achieve is basically a list of the files within the remote directory which I can then mash up into a link which includes the remote IP. The end result is basically a live table of the files in the directory which the user can click on to open the file they need.

I've read about the following method of doing something similar:

basedir = '.'
files = Dir.glob("*.xls")

What I am trying to work out, is how do I make the basedir a remote IP and also how I would build this into my model/controller.

Ideally I would like to do something like this:

file_controller.rb

class FilesController < ApplicationController

 basedir = '192.168.1.1/files/path/to/xlsdocuments/'

 def index
 @xls_files = Dir.glob("*.xls")

 respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @articles }
 end
end

This would then allow me to loop through the @xls_files in my view.

Is this even remotely possible?

UPDATE

Using the above code in my controller as follows, I don't get any errors but I can't figure out how to display the file names:

class DocumentsController < ApplicationController
before_filter :authorize, only: [:new, :edit, :update]
basedir = '192.168.1.1/common/'
  # GET /documents
  # GET /documents.json
  def index
    @documents = Document.all
    @xls_files = Dir.glob("*.xls")
    @xls_files = @xls_files.split('\n')

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @documents }
    end
  end

I'm looping through the file names using the following in my view:

<% @xls_files.each do |xls| %>
 file name
<% end %>

This outputs file name. Any idea how I output the actual filename?

Upvotes: 2

Views: 1173

Answers (2)

dannymcc
dannymcc

Reputation: 3814

For anyone else that may want to do this, these are the steps I took to get it working:

  1. Mount the remote folder locally.

    fusermount -u ~/yourmountdirectory

  2. List the contents of the local (remote) directory

    @xls_files = Dir.glob("/home/danny/nurserotas/*")

3.Output list in view of the files

<ul>
<% @xls_files.each do |xls| %>
<li><%= xls %></li>
<% end %>
</ul>

Upvotes: 2

Dougui
Dougui

Reputation: 7230

Did you try to use a command line like this :

@xls_files = `ssh 192.168.1.1 'ls /path/to/dir *.xls'`
@xls_files = @xls_files.split('\n')

Upvotes: 0

Related Questions