SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Giving a directory listing [RoR]

    I'm trying to display a directory listing, my idea was to populate an array with a list of files, and then display this array. (using the array would also allow me to access the filenames/path's at a later date) This is my current effort, can anyone suggest what the problem is?

    class MyTestController < ApplicationController

    def dirlisting(dirname)
    thelisting=[]
    Dir[dirname].each do |f|
    thelisting.push(f)
    end
    end

    render_text (thelisting[1])

    end

    def index
    render_text "Showing available media"
    dirlisting("\")
    end

    end


    Thanks in advance,
    Paul

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problems:

    1. You have too many "end"s after "dirlisting"
    2. You shouldn't render more than once (render isn't like php's echo)
    3. "\" -- the slash needs to be escaped: "\\"

    Code:
    class MyTestController < ApplicationController
      def index
        render_text "Showing available media: " + Dir["\\"].join("\n")
      end
    end
    Or (better):

    controllers/my_test_controller.rb:
    Code:
    class MyTestController < ApplicationController
      def index
        @dirs = Dir["\\"]
      end
    end
    views/mytest/index.rhtml:
    Code:
    <h1>Available media</h1>
    <ul>
      <% for dir in @dirs %>
      <li><%= dir %></li>
      <% end %>
    </ul>

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried using your (better) code, unfortunately this resulted in no errors, but it just showed the 'Available media' heading.
    In addition, how would I go about referring to individual files, (I later intend to enable them to be clicked which will open the selected file in an embedded media player).
    Would it be simply 'dir[1]' 'dir[2]' etc. ?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this code:

    Code:
    class MyTestController < ApplicationController
      def index
        @dirs = Dir["*"]
      end
    end
    You can create links in the <li>'s to an action that shows the file in a media player:

    Code:
    <%= link_to file, :action => 'show_file_in_player', :id => file %>

  5. #5
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent, thank you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •